0

私は単純なコインフリッププログラムでこの奇妙な問題を抱えています。このコードを実行すると、何らかのエラーが発生するのではなく、クラッシュするだけです。はいまたはいいえの答えを入力して Enter キーを押しますが、何も起こりません。それからもう一度Enterキーを押すと、完全に閉じます。

import time
import random

constant = 1
FirstRun = True

def Intro():
    raw_input("Hello and welcome to the coin flip game. Do you wish to flip a coin? (yes or no): ")


def CoinToss():
    print "You flip the coin"
    time.sleep(1)
    print "and the result is..."
    time.sleep(1)
    result = random.randint(1,2)
    if result == 1:
        print "heads"
    if result == 2:
        print "tails"


while constant == 1:
    if FirstRun == True:
        Intro()
        FirstRun = False

    else:
        answer = raw_input()
        if answer == "yes":
            CoinToss()
            raw_input("Do you want to flip again? (yes or no): ")
        else:
            exit()
4

1 に答える 1

0

メソッドの戻り値を単純に無視しているraw_inputため、ループから抜け出すためにユーザーが入力した内容がわかりません。

これはプログラムの簡略化されたバージョンです。raw_inputメソッドの結果をどのように保存し、resultそれを使用して実行ループを制御するかに注意してください。

import random
import time

result = raw_input('Hello and welcome to the coin flip game. Do you wish to flip a coin? (yes or no): ')

while result != 'no':
    print('You flip the coin....')
    time.sleep(1)
    print('...and the result is...')
    toss_result = random.randint(1,2)
    if toss_result == 1:
        print('Heads')
    else:
        print('Tails')

    result = raw_input('Do you want to flip again? (yes or no): ')

print('Goodbye!')
于 2014-12-09T06:05:03.377 に答える