我三天前才开始学习编程。我决定学习Python,3.7.2版本。今天,我编写了这段代码,试图创建一个游戏,在这个游戏中,你输入一个用户名,然后用石头、纸、剪刀对付一个机器人。然而,在我输入用户名(第2-4行)之后,Python就会关闭。我是通过点击我的资源管理器Windows 10中的文件来运行这个程序的。我想这是一个简单的初学者错误,有人能帮忙吗?
我尝试在第4行添加输入(),当我这样做时,Python在输入用户名后不会关闭。什么都不做,只是换行。当我在换行符中键入什么并按Enter键时,shell将关闭。
这是我的密码:
import random
print('Hello, welcome to Rock, Paper, Scissors. Enter your name to get started.')
usn = input()
def game(choice):
options = ['rock', 'paper', 'scissors']
bot = random.choice(options)
if choice == 'rock' and bot == 'scissors':
print(usn + ' played rock, I played scissors. You won. Nice!')
elif choice == 'rock' and bot == 'paper':
print(usn + ' played rock, I played paper. You lost. Haha, loser!')
elif choice == 'paper' and bot == 'scissors':
print(usn + ' played paper, I played scissors. You lost. Haha, loser!')
elif choice == 'paper' and bot == 'rock':
print(usn + ' played paper, I played rock. You won. Nice!')
elif choice == 'scissors' and bot == 'paper':
print(usn + ' played scissors, I played paper. You won. Nice!')
elif choice == 'scissors' and bot == 'rocks':
print(usn + ' played scissors, I played rocks. You lost. Haha, loser!')
elif choice == bot:
print("It's a draw, dang it!")
def again(x):
while True:
if x == 'Yes':
game(input('Rock, Paper or Scissors?'))
again(input('Want to play again? Yes or No'))
else:
print('Goodbye. Press Enter to exit.')
input()预期:我希望在我输入用户名后,我可以输入岩石,纸或剪刀,然后游戏将发挥。实际结果: Python在输入用户名后关闭。
发布于 2019-01-23 13:30:28
您也需要调用game --尽管此时需要声明game。
我在这里稍微重构了一些东西,使它们对用户来说更方便一些。
game中使用的参数名是username和user_choice,这样就不会隐藏全局名称(或者偶然使用它们)。again()递归函数被一个while循环所取代,当用户说他们不想再次播放时,这个循环就会被break删除。接下来我建议考虑的是如何简化game函数中的重复。:)
import random
def game(username, user_choice):
options = ['rock', 'paper', 'scissors']
bot = random.choice(options)
if user_choice == 'rock' and bot == 'scissors':
print(username + ' played rock, I played scissors. You won. Nice!')
elif user_choice == 'rock' and bot == 'paper':
print(username + ' played rock, I played paper. You lost. Haha, loser!')
elif user_choice == 'paper' and bot == 'scissors':
print(username + ' played paper, I played scissors. You lost. Haha, loser!')
elif user_choice == 'paper' and bot == 'rock':
print(username + ' played paper, I played rock. You won. Nice!')
elif user_choice == 'scissors' and bot == 'paper':
print(username + ' played scissors, I played paper. You won. Nice!')
elif user_choice == 'scissors' and bot == 'rocks':
print(username + ' played scissors, I played rocks. You lost. Haha, loser!')
elif user_choice == bot:
print("It's a draw, dang it!")
print('Hello, welcome to Rock, Paper, Scissors. Enter your name to get started.')
name = input()
while True: # do forever, unless `break`ed out of (or an exception occurs)
choice = input('Rock, Paper or Scissors?').lower().strip()
game(name, choice)
if input('Want to play again? Yes or No').lower().strip() == 'no':
print('Goodbye. Press Enter to exit.')
input()
break # break out of the "while true" loop发布于 2019-01-23 13:30:06
第一个问题是,在收到用户名后,您没有调用任何函数,我会尝试这样做(请注意,我在代码中添加了一些注释):
import random
print('Hello, welcome to Rock, Paper, Scissors. Enter your name to get started.')
usn = input()
def game(choice):
options = ['rock', 'paper', 'scissors']
bot = random.choice(options)
if choice == 'rock' and bot == 'scissors':
print(usn + ' played rock, I played scissors. You won. Nice!')
elif choice == 'rock' and bot == 'paper':
print(usn + ' played rock, I played paper. You lost. Haha, loser!')
elif choice == 'paper' and bot == 'scissors':
print(usn + ' played paper, I played scissors. You lost. Haha, loser!')
elif choice == 'paper' and bot == 'rock':
print(usn + ' played paper, I played rock. You won. Nice!')
elif choice == 'scissors' and bot == 'paper':
print(usn + ' played scissors, I played paper. You won. Nice!')
elif choice == 'scissors' and bot == 'rocks':
print(usn + ' played scissors, I played rocks. You lost. Haha, loser!')
elif choice == bot:
print("It's a draw, dang it!")
# Taking this out of the function, it is going to be called right on the execution
x = 'Yes'
while True: #here you are doing an loop, that will only end when break is called
if x == 'Yes':
game(input('Rock, Paper or Scissors?'))
x = input('Want to play again? Yes or No')
else:
print('Goodbye. Press Enter to exit.')
input()
break发布于 2019-01-23 13:24:55
def game(choice):创建一个名为game的函数。您需要告诉python使用一个参数来执行这个函数:choice,如:game(usn)
https://stackoverflow.com/questions/54328278
复制相似问题