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.

構文の他に、

  • 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 に答える