简体   繁体   中英

Repeat input after choice

I'm new at programming, and my first attempt at a program is a program that can add players to a file named whitelist.txt . I have a menu where the user can choose options and I want it to repeat after an option is chosen. However, the input does not work. This is my code:

 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> : ')

it would be awesome if someone could help me out! thanks!

You need to make a single loop and place all you data saving commands in that. Like:

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!")

You need your entire logic to be inside the loop for it to run forever or until you choose to exit.

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 

I have corrected your programs structure a little, made it a little conventional and solved the issue:

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

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM