修复 while 循环只运行一次

时间:2021-01-11 00:48:43

标签: python

我正在尝试创建一个简单的石头剪刀布游戏来练习一些基本的 Python 技能。我创建了下面的代码来接受用户输入并创建一个简单的基于点的游戏。但是,当我运行代码时,循环无限运行,打印输出消息,直到我强制停止它。如何更改我的代码,以便每个用户输入的 while 循环仅运行一次?

代码:

import random as rand

print("Welcome to Rock, Paper, Scissors!")

x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")

game_choices = ("Rock",
                "Paper",
                "Scissors")

comp_input = rand.choice(game_choices)

phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"

my_score = 0

print("The computer chose: ",comp_input)

while my_score >= 0:                                            
    if x == "R" and comp_input == "Rock":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Scissors":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Paper":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Scissors":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Paper":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Rock":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Paper":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Rock":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Scissors":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break

2 个答案:

答案 0 :(得分:2)

你需要在用户输入之前设置你的 while 循环,否则它只是在相同的输入值上循环:

import random as rand

print("Welcome to Rock, Paper, Scissors!")

my_score = 0

while my_score >= 0:   

    x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")

    game_choices = ("Rock",
                    "Paper",
                    "Scissors")

    comp_input = rand.choice(game_choices)

    phrase_one = "It was a tie!"
    phrase_two = "You win! WOOOOOOO go Grandma!!!"
    phrase_three = "You lost.. shoulda had a V8!"

    print("The computer chose: ",comp_input)
                                         
    if x == "R" and comp_input == "Rock":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Scissors":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Paper":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Scissors":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Paper":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Rock":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Paper":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Rock":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Scissors":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break

下面你顺便得到一个更紧凑的版本:

import random as rand

print("Welcome to Rock, Paper, Scissors!")

phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"

my_score = 0

while my_score >= 0:   

    x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")

    game_choices = ("Rock",
                    "Paper",
                    "Scissors")

    comp_input = rand.choice(game_choices)

    print("The computer chose: ",comp_input)

    #You store the choices in the same list
    match = [x[0],comp_input[0]]

    #set operation provides you with unique elements
    #If the user and comp inputs char are the same, then length = 1
    if len(set(match)) == 1:
        print(phrase_one)
        print("Score: ",my_score)
    #If match in combinations that makes the user win
    elif match in [['R','S'],['S','P'],['P','R']]:
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
    #Other situations are equivalent to loose
    else:
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")

    #You don't need break statements in your code, btw (while condition)

这里,如果用户选择“Rock”,计算机选择“Paper”,则将每个字符串的第一个索引的值存储在名为 match 的列表中。

choice_1 = "Rock"
choice_2 = "Paper"

match = [choice_1[0],choice_2[0]] # match = ["R","P"]

match = set(match) # Yield match = ["R","P"], to test against winning/loosing combinations

choice_1 = "Rock"
choice_2 = "Rock"

match = [choice_1[0],choice_2[0]] # match = ["R","R"]

match = set(match) # Yield match = ["R"], it is a tie because len(match) = 1

答案 1 :(得分:0)

问题在于,除非您在第一轮输掉比赛,否则您将永远获胜,因为所有输入值都没有改变。你所要做的就是将你的 input() 和计算机随机选择移动到这样的循环中

import random as rand

print("Welcome to Rock, Paper, Scissors!")
game_choices = ("Rock",
                    "Paper",
                    "Scissors")



phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"

my_score = 0
while my_score >= 0:
    
    x = input("\nYour move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ").upper()
    comp_input = rand.choice(game_choices)

    print("The computer chose: ",comp_input)

                                            
    if x == "R" and comp_input == "Rock":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Scissors":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Paper":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Scissors":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Paper":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Rock":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Paper":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Rock":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Scissors":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break

相关问题