Python 为什么我会得到;SyntaxError:编译单个语句时发现多个语句;
我正在写一个代码来玩幸运七。在空闲状态下运行模块时,我在“import random”之后立即得到错误。我可以直接在IDLE中输入“importrandom”,它可以正常工作,但是我不能运行整个模块,这意味着我不能测试整个代码 代码如下:Python 为什么我会得到;SyntaxError:编译单个语句时发现多个语句;,python,Python,我正在写一个代码来玩幸运七。在空闲状态下运行模块时,我在“import random”之后立即得到错误。我可以直接在IDLE中输入“importrandom”,它可以正常工作,但是我不能运行整个模块,这意味着我不能测试整个代码 代码如下: import random count = 0 pot = int(input("How much money would you like to start with in your pot? ")) if pot>0 and pot.isdigit
import random
count = 0
pot = int(input("How much money would you like to start with in your pot? "))
if pot>0 and pot.isdigit():
choice = input("Would you like to see the details of where all of your money went? yes, or no? ")
if choice == ("yes", "Yes", "YES", "ya", "Ya", "y", "Y")
while pot>0:
roll1 = random.randint(1, 7)
roll2 = random.randint(1, 7)
roll = (roll1)+(roll2)
count += 1
maxpot = max(pot)
if roll == 7:
pot +=4
print("Your rolled "+str(roll1)" and "+str(roll2)". That's "+str(roll)"! You get $4! Your pot is now at $"+str(pot))
else:
pot -= 1
print("Your rolled "+str(roll1)" and "+str(roll2)". That's "+str(roll)". You loose a dollar... Your pot is now at $"+str(pot))
print("Oh no! Your pot is empty! It took "+str(count)" rounds! Your maximum pot was $"+str(maxpot)"!")
elif choice == ("no", "No", "No", "n", "N"):
while pot>0:
roll1 = random.randint(1, 7)
roll2 = random.randint(1, 7)
roll = (roll1)+(roll2)
count += 1
maxpot = max(pot)
if roll == 7:
pot +=4
else:
pot -= 1
print("Oh no! Your pot is empty! It took "+str(count)" rounds! Your maximum pot was $"+str(maxpot)"!")
else:
print("You did not enter 'yes' or 'no'. Be sure and type either 'yes' or 'no' exactly like that. Lets try agian!")
restart_program
else:
print("Please enter a positive dollar amount.")
restart_program
除了语法之外
- 您似乎重新定义了函数
,因为内置的定义max
无法运行。定义一个与内置函数名重叠的函数是一个非常糟糕的想法max(pot)
- 如果
是一个函数,则应将其称为restart\u program
,否则它是一个不起任何作用的语句restart\u program()
- 在print(…)命令中包含字符串时,有时没有“+”(参见KennyTM的答案,但在每个print()-命令中都缺少这些字符串。)
- pot.isdigit()是胡说八道。pot已经是int,因此函数isdigit不是int的成员
- 未定义重新启动程序(s.KennyTM)
- 马克斯(波特)(s.肯尼特)
if choice == ("yes", "Yes", "YES", "ya", "Ya", "y", "Y")
# ^ you probably should use `in` here.
# ^ and you forgot a ':'.
print("Your rolled "+str(roll1)" and "+str(roll2)". That's "+str(roll)"! You get $4! Your pot is now at $"+str(pot))
# ^ you forgot a `+`. ^ here as well.