gpt4 book ai didi

python - 有没有办法使用 python 创建用户列表来将新用户添加到简单的游戏中?

转载 作者:行者123 更新时间:2023-12-01 07:26:31 26 4
gpt4 key购买 nike

我正在尝试创建一个简单的游戏(掷骰子),允许您添加多个用户,但我遇到了一些问题。现在我的问题是我无法从嵌套循环中正确输出列表。 (我知道需要打印该值,但我有点卡住)

import random
import time

def game_start():
welcome = input("Welcome new user, would you like to play a game?")

if welcome in ('y', 'yes', 'YES', 'Y'):
print("Let's begin...")
time.sleep(1)
users = login()
players = game(users)
else:
print("What a drag...")


def login():
while True:
try:
user_count = int(input("Enter the number of users that wish to play..."))
time.sleep(1)
print("The number of user is: ", user_count)

username = []

for i in range(1,user_count + 1):
username.append(input(f"What is player {i} name?"))
print(f'Welcome, {username} you have been successfully logged in.')

except ValueError:
print("Oops! That was no vaild number. Try again...")

def game(users):
print(f'This is returning the list of players {users}')

def roll():
die1 = random.randit(1,6)
die2 = random.randit(1,6)
change = 10 if (die1 + die2) % 2 == 0 else -5
points = die1 + die2 + change
if die1 == die2:
points += randit(1,6)
return points


def main():
game_start()


if __name__ == '__main__':
main()

预期结果:要打印的列表“这是返回玩家列表”实际结果:陷入 while 循环

最佳答案

正如我在评论中提到的,似乎存在两个问题。第一个是 login() 函数内部有一个无限循环。 while(true) 永远为真。在下面的代码中,我将用于检索用户数量的用户输入的函数分离到其自己的函数中。该函数利用了 python 没有显式类型的事实(意味着您可以更改同一变量的类型)。

第二个问题是您的 login() 函数没有返回任何内容。这意味着在 game_start() 函数内部,users=login() 行将始终为 null。这将使您的下一行失败,因为您没有用户列表。我在下面添加了返回语句。

与原始发布的解决方案接近

def login():
while True:
try:
user_count = int(input("Enter the number of users that wish to play..."))
print("The number of user is: ", user_count)

username = []

for i in range(1,user_count + 1):
username.append(input(f"What is player {i} name?"))
print(f'Welcome, {username[i-1]} you have been successfully logged in.')
return username

except ValueError:
print("Oops! That was no vaild number. Try again...")

利用非显式类型解决问题的旧方法============================================

def getUserCount():
user="NULL"
while(type(user)!= int):
userTemp=input("Enter the number of users that wish to play: ")
try:
user=int(userTemp)
except ValueError:
print("Oops! That was no vaild number. Try again...")
return user

def login():
user_count=getUserCount()
print("The number of user is: ", user_count)
username = []
for i in range(1,user_count + 1):
username.append(input(f"Enter player {i} name: "))
print(f'Welcome, {username} you have been successfully logged in.')
return username

这是仅包含这两个函数的示例输出:

>>> login()
Enter the number of users that wish to play: nope
Oops! That was no vaild number. Try again...
Enter the number of users that wish to play: not
Oops! That was no vaild number. Try again...
Enter the number of users that wish to play: gonna
Oops! That was no vaild number. Try again...
Enter the number of users that wish to play: work
Oops! That was no vaild number. Try again...
Enter the number of users that wish to play: 3
The number of user is: 3
Enter player 1 name: one
Welcome, ['one'] you have been successfully logged in.
Enter player 2 name: two
Welcome, ['one', 'two'] you have been successfully logged in.
Enter player 3 name: three
Welcome, ['one', 'two', 'three'] you have been successfully logged in.
['one', 'two', 'three']

如您所见,在 login() 函数内打印用户名会打印整个数组,我确信这是不需要的。我已经使用下面的 login() 定义修复了该问题。

def login():
user_count=getUserCount()
print("The number of user is: ", user_count)
username = []
for i in range(1,user_count + 1):
username.append(input(f"Enter player {i} name: "))
#username[i-1] since i starts at 1 and not 0
print(f'Welcome, {username[i-1]} you have been successfully logged in.')
return username

提供示例输出:

>>> login()
Enter the number of users that wish to play: strings?
Oops! That was no vaild number. Try again...
Enter the number of users that wish to play: *@#
Oops! That was no vaild number. Try again...
Enter the number of users that wish to play: 3
The number of user is: 3
Enter player 1 name: one
Welcome, one you have been successfully logged in.
Enter player 2 name: four
Welcome, four you have been successfully logged in.
Enter player 3 name: seven
Welcome, seven you have been successfully logged in.
['one', 'four', 'seven']

关于python - 有没有办法使用 python 创建用户列表来将新用户添加到简单的游戏中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57416403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com