随机整数并接收并保存它们
random integers and receiving them and saving them
所以我是 Python 的新手,我一直在为 class 的一节课掷骰子,我真的坚持了下来。除了掷骰子发生的那一点,我的代码工作得很好。如果你掷出 6,那么它应该打印出我赢了。但它打印出我丢失了。我不知道为什么会这样,我们将不胜感激。它旨在询问您的姓名,给您一个用户名,然后询问您是否想玩。但最后一切都出错了,你得到的号码。就像我说的那样,我很感激测试
from random import randint
print ("Hello, whats is your first name?")
first_name = input()
print ("What is your second name?")
second_name = input()
username = first_name[0] + first_name[1] + second_name[0] + second_name[1]
print (username + ", I want to play a game. Do you accept?")
game = input()
if game == ("yes") or ("Yes") or :
print ("Great, let's play. I'm going to roll a dice, if it lands on a 6, you win. If")
print ("not, you lose.")
dice_roll = print (randint(1,6))
if dice_roll == 6:
print ("Congrats, you win.")
elif dice_roll != 6:
print ("Sorry. You lose.")
elif game == ("no") or ("No"):
print ("Ok, Bye.")
else:
print ("Ok, Bye.")
你为什么要这样做
dice_roll = print (randint(1,6))
这将打印到控制台和 return none。所以,dice_roll 总是 None。所以,将其更改为以下
dice_roll = randint(1,6)
要打印它,只需将它打印在单独的一行上。
还有一个问题是下面一行
if game == ("yes") or ("Yes") or :
你不能那样做。在 "or" 之后你必须提到一些事情。因此,将其更改为以下
if game == "yes" or game == "Yes" :
改变滚动部分喜欢打印结果,不是将打印分配给变量——而是值本身:
dice_roll = randint(1,6)
print(dice_roll)
if dice_roll == 6:
print ("Congrats, you win.")
elif dice_roll != 6:
print ("Sorry. You lose.")
elif game == ("no") or ("No"):
print ("Ok, Bye.")
else:
print ("Ok, Bye.")
您需要更改此行:
dice_roll = print (randint(1,6))
因为你将dice_roll参数赋值给打印语句,如果你使用:
dice_roll = (randint(1,6))
print(dice_roll)
一切都应该没问题。
此外,您可以通过更改(并删除两个 "or" 语句)来精简代码:
if game == ("yes") or ("Yes") or :
至:
if game.lower() == "yes":
这将处理大量字母异常
所以我是 Python 的新手,我一直在为 class 的一节课掷骰子,我真的坚持了下来。除了掷骰子发生的那一点,我的代码工作得很好。如果你掷出 6,那么它应该打印出我赢了。但它打印出我丢失了。我不知道为什么会这样,我们将不胜感激。它旨在询问您的姓名,给您一个用户名,然后询问您是否想玩。但最后一切都出错了,你得到的号码。就像我说的那样,我很感激测试
from random import randint
print ("Hello, whats is your first name?")
first_name = input()
print ("What is your second name?")
second_name = input()
username = first_name[0] + first_name[1] + second_name[0] + second_name[1]
print (username + ", I want to play a game. Do you accept?")
game = input()
if game == ("yes") or ("Yes") or :
print ("Great, let's play. I'm going to roll a dice, if it lands on a 6, you win. If")
print ("not, you lose.")
dice_roll = print (randint(1,6))
if dice_roll == 6:
print ("Congrats, you win.")
elif dice_roll != 6:
print ("Sorry. You lose.")
elif game == ("no") or ("No"):
print ("Ok, Bye.")
else:
print ("Ok, Bye.")
你为什么要这样做
dice_roll = print (randint(1,6))
这将打印到控制台和 return none。所以,dice_roll 总是 None。所以,将其更改为以下
dice_roll = randint(1,6)
要打印它,只需将它打印在单独的一行上。
还有一个问题是下面一行
if game == ("yes") or ("Yes") or :
你不能那样做。在 "or" 之后你必须提到一些事情。因此,将其更改为以下
if game == "yes" or game == "Yes" :
改变滚动部分喜欢打印结果,不是将打印分配给变量——而是值本身:
dice_roll = randint(1,6)
print(dice_roll)
if dice_roll == 6:
print ("Congrats, you win.")
elif dice_roll != 6:
print ("Sorry. You lose.")
elif game == ("no") or ("No"):
print ("Ok, Bye.")
else:
print ("Ok, Bye.")
您需要更改此行:
dice_roll = print (randint(1,6))
因为你将dice_roll参数赋值给打印语句,如果你使用:
dice_roll = (randint(1,6))
print(dice_roll)
一切都应该没问题。
此外,您可以通过更改(并删除两个 "or" 语句)来精简代码:
if game == ("yes") or ("Yes") or :
至:
if game.lower() == "yes":
这将处理大量字母异常