0

我正在编写一个代码来玩幸运七人制。在 IDLE 中运行模块时,我在“导入随机”之后立即收到错误。我可以直接在 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
4

2 回答 2

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.

除了语法,

  • 看来您已经重新定义了 function ,因为无法运行max内置定义。max(pot)定义一个与内置名称重叠的函数是一个非常糟糕的主意。
  • 如果restart_program是一个函数,你应该把它称为restart_program(),否则它是一个什么都不做的语句。
于 2013-09-15T18:27:55.417 回答
0
  • 在 print(...) 命令中连接字符串时,有几次没有“+”(s. KennyTM 的回答,但在每个 print() 命令中都缺少它们。)
  • pot.isdigit() 是胡说八道。pot 已经是一个 int,因此函数 isdigit 不是 int 的成员。
  • restart_programm 未定义(s. KennyTM)
  • max(pot) (s. KennyTM)
于 2013-09-15T19:05:47.947 回答