根据用户输入的循环程序

时间:2019-05-09 02:23:04

标签: python if-statement

我正在制作一个Rock,Paper,Scissors游戏,游戏结束后它会在屏幕上显示“您想再玩一次吗?”。当用户选择yes时,我正努力让游戏再次运行。 。当用户选择否时,我可以让程序说再见。

如果用户多次选择“是”,我可以将代码复制并粘贴到If语句中,但这效率很低,我觉得必须有一种更简单的方法。

import random
selection = ("Rock", "Paper", "Scissors")
user1 = input("Pick Rock, Paper, or Scissors: ").upper()
computer = random.choice(selection)
if (user1 == "ROCK"):
        if (computer == "Paper"):
            print ("The computer selected: " + (computer))
            print ("You lost")
        elif (computer == "Scissors"):
            print ("The computer selected: " + (computer))
            print ("You win")
        elif (computer == "Rock"):
            print ("The computer selected: " + (computer))
            print ("Tie")
        else:
            print ("Uh oh something went wrong. Please try again")
elif (user1 == "PAPER"):
            if (computer == "Scissors"):
                print ("The computer selected: " + (computer))
                print ("You lost")
            elif (computer == "Rock"):
                print ("The computer selected: " + (computer))
                print ("You win")
            elif (computer == "Paper"):
                print ("The computer selected: " + (computer))
                print ("Tie")
            else:
                print ("Uh oh something went wrong. Please try again")
elif (user1 == "SCISSORS"):
        if (computer == "Rock"):
            print ("The computer selected: " + (computer))
            print ("You lost")
        elif (computer == "Paper"):
            print ("The computer selected: " + (computer))
            print ("You win")
        elif (computer == "Scissors"):
            print ("The computer selected: " + (computer))
            print ("Tie")
        else:
                print ("Uh oh something went wrong. Please try again")
else:
            print ("Invalid Selection")
while True:
    # main program
    while True:
        answer = input('Run again? (y/n): ')
        if answer in ('y', 'n'):
            break
        print ('Invalid input.')
    if answer == 'y':
        continue
    else:
        print ('Goodbye')
        break

我的预期结果是,当用户键入“ y”并点击进入游戏时,游戏将再次开始,但是到目前为止,当用户键入“ y”时,它只会重复相同的“您想再次玩吗。我知道那是因为我在If语句中没有任何实际代码,但是我不知道需要添加什么内容才能获得所需的结果。

3 个答案:

答案 0 :(得分:0)

当您键入Continue时,它将不断重复该语句

这是您代码的解决方案

import random


while Continue:
    selection = ("Rock", "Paper", "Scissors")
    user1 = input("Pick Rock, Paper, or Scissors: ").upper()
    computer = random.choice(selection)
    if (user1 == "ROCK"):
            if (computer == "Paper"):
                print ("The computer selected: " + (computer))
                print ("You lost")
            elif (computer == "Scissors"):
                print ("The computer selected: " + (computer))
                print ("You win")
            elif (computer == "Rock"):
                print ("The computer selected: " + (computer))
                print ("Tie")
            else:
                print ("Uh oh something went wrong. Please try again")
    elif (user1 == "PAPER"):
                if (computer == "Scissors"):
                    print ("The computer selected: " + (computer))
                    print ("You lost")
                elif (computer == "Rock"):
                    print ("The computer selected: " + (computer))
                    print ("You win")
                elif (computer == "Paper"):
                    print ("The computer selected: " + (computer))
                    print ("Tie")
                else:
                    print ("Uh oh something went wrong. Please try again")
    elif (user1 == "SCISSORS"):
            if (computer == "Rock"):
                print ("The computer selected: " + (computer))
                print ("You lost")
            elif (computer == "Paper"):
                print ("The computer selected: " + (computer))
                print ("You win")
            elif (computer == "Scissors"):
                print ("The computer selected: " + (computer))
                print ("Tie")
            else:
                print ("Uh oh something went wrong. Please try again")
    else:
        print ("Invalid Selection")

    answer = input('Run again? (y/n): ')
    while True:
        if answer in ('y', 'n'):
            print ('Invalid input.')
        if answer == 'y':
            Continue = True
        else:
            print ('Goodbye')
            Continue = False
            break

答案 1 :(得分:0)

您可以定义一个函数:

def play_game():
    selection = ("Rock", "Paper", "Scissors")
    user1 = input("Pick Rock, Paper, or Scissors: ").upper()
    computer = random.choice(selection)
    # ...
    # ...
    # ...
    else:
        print ("Invalid Selection")

并根据循环中的用户输入进行调用:

play_game()    
while True:
    answer = input('Run again? (y/n): ')
    if answer.lower() == 'y':
        play_game()
    elif answer.lower() == 'n':
        break
    else:
        print ('Invalid input.')

答案 2 :(得分:0)

如果需要,您还可以在游戏功能中加入while循环

import random
import time

class RockPaperScissors:

    def __init__(self):
        self.elements = ['rock', 'paper', 'scissors']
        self.valid_inputs = ['1','2','3']

    def win_checker(self, cpu_choice, user_choice):
        if ((user_choice == '2' and cpu_choice == 'rock')
        or (user_choice == '1' and cpu_choice == 'scissors')
        or(user_choice == '3' and cpu_choice == 'paper')):
            print('YOU WON')
        else:
            print('You Lost. Computer chose, ', cpu_choice.upper())

    def input_validator(self, user_input):
        if user_input in self.valid_inputs:
            return True

    def play_again(self):
       if input("Do You Want To Play Again? Y/N?").lower() == 'y':
           return True
       else: return False

    def game(self):
        for option in self.elements:
            print(option)
            time.sleep(1)
        user_guess = input('1: Rock, 2:Paper, 3:Scissors: ')
        if self.input_validator == True:
            cpu_choice = random.choice(self.elements)
            self.win_checker(cpu_choice,user_guess)
        else: print('Invalid Input')

play_again = True
while play_again == True:      
    RockPaperScissors().game()
    play_again = RockPaperScissors().play_again()