0

ユーザーに 1 ~ 100 の数字を入力するよう求めるプログラムを作成すると、プログラムは、これらの数字が高すぎるか低すぎる場合、および勝った場合にユーザーに通知します。勝ったら、もう一度プレイするかやめるかを尋ねられます。問題は、プログラムにゲームをリプレイさせる方法がわからないことです。助けていただければ幸いです (そして、ほとんどの人が def を使いたいと思っていることはわかっていますが、私はその使い方がわからないので、使っていない人がいれば幸いです) ありがとうございます。

import random
count=0
user=raw_input("Welcome to Guess the Number! Please enter a number from 1-100: ")
user=int(float(user))
computer=random.randrange(0,101)
computer=int(float(computer))
while user!=computer:
    if user<computer:
        user=raw_input("This number is too low! Please try again: ")
        user=int(float(user))
        count+=1
    if user>computer:
        user=raw_input("This number is too high! Please try again: ")
        user=int(float(user))
        count+=1
else:
    count+=1
    print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " tries to get the right answer!"
    user=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
    while user!="play" and user1!="stop":
        user=raw_input("Thats not what I asked for! If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
        if user=="play":
            count=0
            computer=random.randrange(0,101)
            computer=int(float(computer))
            while user!=computer:
                if user<computer:
                    user=raw_input("This number is too low! Please try again: ")
                    user=int(float(user))
                    count+=1
                if user>computer:
                    user=raw_input("This number is too high! Please try again: ")
                    user=int(float(user))
                    count+=1
            else:
                count+=1
                print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " to get the right answer!"
                user=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
        if user=="stop":
            print ""
4

2 に答える 2

3
import random

def play_game():

    # Much nicer than int(float(random.randrange(0,101)))
    computer = random.randint(0, 101)
    count = 0

    # Keep looping until we return
    while True:
        
        count += 1
        user = int(raw_input('Please guess a number: '))

        if user < computer:
            print('too low!')
        elif user > computer:
            print('too high!')
        else:
            print('You win!')
            print('It took you {} tries to get the right answer!'.format(count)) 
            return  # Leave play_game

def main():
    
    print('Welcome!')

    while True:    
        play_game()

        play_again = raw_input('Play again? y/n: ') == 'y'
        if not play_again:
            return  # Leave main

main()
于 2014-11-13T02:12:48.433 に答える
0
import random
count=0
user=raw_input("Welcome to Guess the Number! Please enter a number from 1-100: ")

go = False

while(go is True):
    user=int(float(user))
    computer=random.randrange(0,101)
    computer=int(float(computer))
    while user!=computer:
        if user<computer:
            user=raw_input("This number is too low! Please try again: ")
            user=int(float(user))
            count+=1
        if user>computer:
            user=raw_input("This number is too high! Please try again: ")
            user=int(float(user))
            count+=1
    else:
        count+=1
        print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " tries to get the right answer!"
        user1=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
        while user!="play" and user1!="stop":
            user1=raw_input("Thats not what I asked for! If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
            if user=="play":
                count=0
                computer=random.randrange(0,101)
                computer=int(float(computer))
                while user!=computer:
                    if user<computer:
                        user=raw_input("This number is too low! Please try again: ")
                        user=int(float(user))
                        count+=1
                    if user>computer:
                        user=raw_input("This number is too high! Please try again: ")
                        user=int(float(user))
                        count+=1
                else:
                    count+=1
                    print "You win! The computer entered: " + str(computer) + " It took you " + str(count) + " to get the right answer!"
                    user=raw_input("If you would like to play again, please enter 'play' and if you would like to stop, please enter 'stop': ")
            if user=="stop":
                #print ""
                #Change it so that you change go to False
                #The loop will not execute again
                go = False

このコードが機能すると仮定すると (私は実行しませんでした)、抜け出すまで実行されるある種のループにラップすることになります。この場合、go というブール値をテストする while ループを使用しました。本来は true で、while ループが何度も繰り返されることを意味しますが、ユーザーが停止したい場合は、go を False に設定して処理します。go が false になったため while ループは実行されず、while ループの後に実行するものが他にないため、プログラムは終了します。

于 2014-11-13T02:09:20.170 に答える