What's new

Help Java to Python conversion help

kenmochin_

black company
Contributor
Joined
Jan 10, 2021
Posts
3,030
Solutions
283
Reaction
14,964
Points
8,202
May java code samin na pinapagawa pero nahihirapan talaga ako i convert to python any idea pano iconvert to sa python huhu salamat!

Code

import java.util.Scanner; import java.util.PriorityQueue; public class Greeting { public static void main(String args[]) { PriorityQueue<String> prio = new PriorityQueue<>(); Scanner scanner = new Scanner(System.in); System.out.println("Enter the nicknames of your classmates: "); for(int x = 0; x < 4; x++) prio.offer(scanner.nextLine()); System.out.print("Press H to say Hi to each of them. "); do { char Hi = Character.toUpperCase(scanner.next().charAt(0)); if(!prio.isEmpty()) System.out.println("Hi " + prio.remove()); else System.out.print("Done saying Hi"); }while(true); } }
 
Try
Note: hindi siya translated line by line. Ni-test ko lang muna yang java kung anong main function niya.
Pwede rin gawin mo muna bago mo test tong ginawa ko.
As long as you know how the function works, hindi kana mahihirapan itranslate.

Python:
names = []

for _ in range(4):
    names.append(input('Enter the nicknames of your classmates: '))

if input('Press H to say Hi to each of them. ').upper() == 'H':
    for name in names:
        print(f'Hi {name}')
    print('Done saying Hi')

or para hindi paulit-ulit yung Enter the nicknames

Python:
names = []

print('Enter the nicknames of your classmates: ')
for _ in range(4):
    names.append(input('>>> '))

if input('Press H to say Hi to each of them. ').upper() == 'H':
    for name in names:
        print(f'Hi {name}')
    print('Done saying Hi')

or list comprehension

Python:
print('Enter the nicknames of your classmates: ')
names = [input('>>> ') for _ in range(4)]

if input('Press H to say Hi to each of them. ').upper() == 'H':
    print(*(f'Hi {name}' for name in names), sep='\n')
    print('Done saying Hi')

Pwede mo rin check kung hindi empty yung names.

Python:
if names and input('Press H to say Hi to each of them. ').upper() == 'H':

And ikaw na bahala sa else kung hindi H yung input ng user. pwede mong gamitan ng While loop, while the user's input is not h/H
 
Last edited:
Try
Note: hindi siya translated line by line. Ni-test ko lang muna yang java kung anong main function niya.
Pwede rin gawin mo muna bago mo test tong ginawa ko.
As long as you know how the function works, hindi kana mahihirapan itranslate.

Python:
names = []

for _ in range(4):
    names.append(input('Enter the nicknames of your classmates: '))

if input('Press H to say Hi to each of them. ').upper() == 'H':
    for name in names:
        print(f'Hi {name}')
    print('Done saying Hi')

or para hindi paulit-ulit yung Enter the nicknames

Python:
names = []

print('Enter the nicknames of your classmates: ')
for _ in range(4):
    names.append(input('>>> '))

if input('Press H to say Hi to each of them. ').upper() == 'H':
    for name in names:
        print(f'Hi {name}')
    print('Done saying Hi')

or list comprehension

Python:
print('Enter the nicknames of your classmates: ')
names = [input('>>> ') for _ in range(4)]

if input('Press H to say Hi to each of them. ').upper() == 'H':
    print(*(f'Hi {name}' for name in names), sep='\n')
    print('Done saying Hi')

Pwede mo rin check kung hindi empty yung names.

Python:
if names and input('Press H to say Hi to each of them. ').upper() == 'H':

And ikaw na bahala sa else kung hindi H yung input ng user. pwede mong gamitan ng While loop, while the user's input is not h/H
omg nagegets ko na sir sdalamat dito sobra napaka helpful
 
omg nagegets ko na sir sdalamat dito sobra napaka helpful
Welcome lang.
Basta next time, para matranslate mo ng madali, test mo muna yung java kung paano nagwowork yung function niya. Kasi kung itatranslate mo line by line mahihirapan ka talaga.
 

Similar threads

Back
Top