๐Ÿ”’ Closed Java to Python conversion help

Status
Not open for further replies.

kenmochin_

black company
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
 
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
 
Status
Not open for further replies.

About this Thread

  • 3
    Replies
  • 2K
    Views
  • 2
    Participants
Last reply from:
zackmark29

Online now

Members online
1,026
Guests online
718
Total visitors
1,744

Forum statistics

Threads
2,276,962
Posts
28,973,362
Members
1,229,667
Latest member
Kismut26
Back
Top