为什么我得到“SyntaxError:编译单个语句时发现多个语句”

时间:2013-09-15 18:25:48

标签: python

我正在写一个代码来玩幸运的七人制。在IDLE中运行模块时,我在“import random”后立即收到错误。我可以直接在IDLE中输入“import random”,它可以正常工作,但我无法运行整个模块,这意味着我无法测试整个代码。

下面是代码:

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

2 个答案:

答案 0 :(得分:2)

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.

除语法外,

  • 您似乎已重新定义了函数max,因为无法运行内置定义max(pot)。定义一个与内置名称重叠的函数是一个非常糟糕的主意。
  • 如果restart_program是一个函数,则应将其称为restart_program(),否则它将是一个不执行任何操作的语句。

答案 1 :(得分:0)

  • 在你的print(...)命令中集合字符串时,有几次没有“+”(s.KennyTM的答案,但是在每个print() - 命令中都缺少它们。)
  • pot.isdigit()是胡说八道。 pot已经是一个int,因此函数isdigit不是int的成员。
  • restart_programm未定义(s.KennyTM)
  • max(pot)(s.KennyTM)