Python 选择后重复输入
我是编程新手,我第一次尝试的程序是一个可以将玩家添加到名为Python 选择后重复输入,python,python-3.x,Python,Python 3.x,我是编程新手,我第一次尝试的程序是一个可以将玩家添加到名为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> : ')
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
希望有帮助 你能指定“它不工作”是什么意思吗?是的,选项列表会出现,但在我输入后:比如说1,选项会一次又一次地出现。但是我不能输入任何东西
我不能输入任何东西。
。你这是什么意思?我想在我添加了一个玩家后重复选择列表。这在第一次使用时效果很好。但是在列表重复之后,我可以输入1,但是列表再次重复。我无法添加其他玩家,只是列表会非常精确地重复!我可能在你也在的时候写下了答案!我的几乎一样+祝你好运