簡體   English   中英

選擇后重復輸入

[英]Repeat input after choice

我是編程新手,我對程序的第一次嘗試是一個可以將玩家添加到名為whitelist.txt的文件的程序。 我有一個菜單,用戶可以在其中選擇選項,並且我希望它在選擇一個選項后重復。 但是,輸入不起作用。 這是我的代碼:

 stuck_forever = True
 home_input = input('What you like to do?' + '\n 1) add a user to the whitelist' + '\n 2) list users in whitelist' + '\n> : ')

# appends the input to the whitelist text file
if home_input == '1':
     a_whitelist = open ('whitelist.txt','a')
     input_username = input('Please enter the username you would like to whitelist: ')
     append_username = input_username + '\n'
     a_whitelist.write(append_username)
     a_whitelist.close()
     print('User added to the whitelist!\n')

while stuck_forever == True:
    home_input = input('What you like to do?' + '\n 1) add a user to the whitelist' + '\n 2) list users in whitelist' + '\n> : ')    

# prints the whitelist and counts the amount of players in the list
if home_input == '2':
   open_whitelist = open('whitelist.txt','r')
   r_whitelist = open_whitelist.read()
   number_users = len(r_whitelist.split())
   print(f'\nThere are {number_users} players whitelisted at the moment..' + '\n')
   print(r_whitelist)
   open_whitelist.close()

while stuck_forever == True:
    home_input = input('What you like to do?' + '\n 1) add a user to the whitelist' + '\n 2) list users in whitelist' + '\n> : ')

如果有人可以幫助我,那就太棒了! 謝謝!

您需要創建一個循環並將所有數據保存命令放在其中。 喜歡:

stuck_forever = True
while stuck_forever == True:
    home_input = input('What you like to do?' + '\n 1) add a user to the whitelist' + '\n 2) list users in whitelist' + '\n> : ')
    if home_input == '1':
        a_whitelist = open ('whitelist.txt','a')
        input_username = input('Please enter the username you would like to whitelist: ')
        append_username = input_username + '\n'
        a_whitelist.write(append_username)
        a_whitelist.close()
        print('User added to the whitelist!\n')

    elif home_input == '2':
       open_whitelist = open('whitelist.txt','r')
       r_whitelist = open_whitelist.read()
       number_users = len(r_whitelist.split())
       print(f'\nThere are {number_users} players whitelisted at the moment..' + '\n')
       print(r_whitelist)
       open_whitelist.close()
    elif home_input == "exit":
        break
    else:
        print("Invalid Choice!")

您需要將整個邏輯置於循環內,以便它永遠運行或直到您選擇退出。

while True:
    home_input = input('What you like to do? \n 1) add a user to the whitelist \n 2) list users in whitelist 3) exit : ')
    if home_input == '1':
        a_whitelist = open ('whitelist.txt','a')
        input_username = input('Please enter the username you would like to whitelist: ')
        append_username = input_username + '\n'
        a_whitelist.write(append_username)
        a_whitelist.close()
        print('User added to the whitelist!\n')
    if home_input == '2':
        open_whitelist = open('whitelist.txt','r')
        r_whitelist = open_whitelist.read()
        number_users = len(r_whitelist.split())
        print(f'\nThere are {number_users} players whitelisted at the moment..\n')
        print(r_whitelist)
        open_whitelist.close()
    if home_input == '3':
        break 

我稍微修正了你的程序結構,使它有點傳統並解決了這個問題:

stuck_forever = True

while stuck_forever == True:
    home_input = input('What you like to do?' + '\n 1) add a user to the whitelist' + '\n 2) list users in whitelist'+'\n 3) Exit' + '\n> : ')

    if home_input == '1':
        a_whitelist = open ('whitelist.txt','a')
        input_username = input('Please enter the username you would like to whitelist: ')
        append_username = input_username + '\n'
        a_whitelist.write(append_username)
        a_whitelist.close()
        print('User added to the whitelist!\n')
    elif home_input == '2':
        open_whitelist = open('whitelist.txt','r')
        r_whitelist = open_whitelist.read()
        number_users = len(r_whitelist.split())
        print('\nThere are {number_users} players whitelisted at the moment..' + '\n')
        print(r_whitelist)
        open_whitelist.close()
    # To exit the loop
    elif home_input == '3':
        break

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM