我今天已经开始玩弄Python,这是我生命中的第一次,我决定制作某种彩票“游戏”,其中预先设置了注册用户列表,即需要验证的“玩家”通过使用“玩家”变量。如何使用循环使用播放器变量输入并搜索列表播放器中是否存在该变量? 可能这是一个非常愚蠢的问题,但是很好奇如何做到这一点,或者是否还有另一种更好/更快的方式,我希望有人启发我。谢谢:
lucky_numbers = [4, 8, 15, 17, 23, 42]
players = ["Kevin", "Stacey", "Jim", "Monica", "Donnie"]
player = input("Please enter your name: ")
if player in players:
print(f"Ok {player}, move on..")
else:
print(f"Uh oh, you are not on the players list!")
input_numbers = list(map(int, input("Enter 6 lucky numbers: ").split()))
if input_numbers == lucky_numbers:
print("Congratulations! YOU WON!")
else:
print("Sorry. You guessed wrong!")
答案 0 :(得分:0)
错误在于使用该行
player = print(input("Please enter your name: "))
如果您打印播放器变量,则不会分配。
应该是
player = input("Please enter your name: ")
代替。
答案 1 :(得分:0)
对不起,我读了好几次,但我还是不太明白你的意思。
是否要让用户重复输入名称,直到名称出现在列表中?名称在列表中后,就可以进入下一阶段了吗?
lucky_numbers = [4, 8, 15, 17, 23, 42]
players = ["Kevin", "Stacey", "Jim", "Monica", "Donnie"]
is_login_success = False
while True:
name = input("Please enter your name or enter `Exit` to exit game\n> ").capitalize().strip()
if name == "Exit":
is_login_success = False
print("Bye bye! Hope see you soon.")
break
elif name in players:
is_login_success = True
print(f"Ok {name}, move on..")
break
else:
print("Uh oh, you are not on the players list!")
print()
if is_login_success:
# play game
此外,对游戏提出一些建议: