Python 巨蟒石、布、剪刀游戏没有按预期运行

Python 巨蟒石、布、剪刀游戏没有按预期运行,python,Python,我是python的完全初学者,我只是在尝试创建一个石头、布、剪刀的游戏,但我认为我犯了一些错误,因为程序似乎没有按预期工作。当我给x一个值后,它显示你输了比赛;它甚至没有通过循环 print('welcome to the virtual game of rock,paper,scissors!') print('select your move:rock,paper,scissors') x = '' input(x) import random y = random.choice(['roc

我是python的完全初学者,我只是在尝试创建一个石头、布、剪刀的游戏,但我认为我犯了一些错误,因为程序似乎没有按预期工作。当我给x一个值后,它显示你输了比赛;它甚至没有通过循环

print('welcome to the virtual game of rock,paper,scissors!')
print('select your move:rock,paper,scissors')
x = ''
input(x)
import random
y = random.choice(['rock', 'paper', 'scissors'])
wins = 0
loses = 0
for i in range(3):
 if x == 'rock':
     if y == 'rock':
         print('the computer used')
         print(y)
         print('draw')
         print(wins)
         print(loses)
     elif y == 'paper':
         print('the computer used')
         print(y)
         print('you lost this round')
         loses = loses+1
         print(wins)
         print(loses)
     elif y == 'scissors':
         print('the computer used')
         print(y)
         print('you won this round')
         wins = wins+1
         print(wins)
         print(loses)
 if x == 'paper':
     if y == 'rock':
         print('the computer used')
         print(y)
         print('you won this round')
         wins = wins+1
         print(wins)
         print(loses)
     elif y == 'paper':
         print('the computer used')
         print(y)
         print('draw')
         print(wins)
         print(loses)
     elif y == 'scissors':
         print('the computer used')
         print(y)
         print('you lost this round')
         loses = loses+1
         print(wins)
         print(loses)
 if x == 'scissors':
     if y == 'rock':
         print('the computer used')
         print(y)
         print('you lost this round')
         loses = loses+1
         print(wins)
         print(loses)
     elif  y == 'paper':
         print('the computer used')
         print(y)
         print('you won this round')
         wins = wins+1
         print(wins)
         print(loses)
     elif y == 'scissors':
         print('the computer used')
         print(y)
         print('draw')
         print(wins)
         print(loses)
if wins > loses:
 print('you won the game')
else:
 print('you lost the game')

您的输入语句应该是一个赋值:

x = input('select your move: rock, paper, scissors')
代码中还有一些其他问题。例如,y(计算机移动)在每一轮之间不会改变。x(玩家移动)也不移动。跨3行的打印语句可以压缩为1行。你可能想用一本字典来获得摇滚乐/纸片/剪刀组合的胜负,而不是对每一个案例进行编程

这是一个示例解决方案。尝试了解它是如何工作的,在继续之前做了哪些更改

import random

moveDict = {'rock': 'paper', 'paper': 'scissors', 'scissors': 'rock'}
# A dictionary is defined with the winning move for each possible move.

print('welcome to the virtual game of rock,paper,scissors!')
wins = 0
loses = 0
for i in range(3):
    #We get a new user move and computer move for each of the three rounds we play
    x = input('select your move: rock, paper, scissors')
    y = random.choice(['rock', 'paper', 'scissors'])  
    #We check using the dictionary if the player played the winning move against a computers move      
    if moveDict[y] == x:
        print('the computer used ' + y + '. You won.')
        wins += 1
    #We check if the player played the same move as the computer
    elif y == x:
        print('the computer used ' + y + '. You tied')
    else:
        print('the computer used ' + y + '. You lost')
        loses += 1
    print('Wins: ' + str(wins) + '\nLosses: ' + str(loses))
if (wins > loses):
    print('you won the game')
elif (wins == losses):
    print('you tied')
else:
    print('you lost the game')

要从用户处读取值,需要获得如下
input
函数的结果

x = input('select your move:rock,paper,scissors: ')
然后,当你想玩几次时,你需要询问循环中的用户,以及循环中的随机选择

for i in range(3):
    x = input('select your move:rock,paper,scissors: ')
    y = random.choice(['rock', 'paper', 'scissors'])
    # ...

此外,我建议您结合一些打印,以使代码更易于阅读

for i in range(3):
    x = input('select your move:rock,paper,scissors: ')
    y = random.choice(['rock', 'paper', 'scissors'])
    if x == 'rock':
        if y == 'rock':
            print('the computer used', y, '=> draw. Score', wins, "/", loses)
        elif y == 'paper':
            loses = loses + 1
            print('the computer used', y, '=> you lost. Score', wins, "/", loses)
        elif y == 'scissors':
            wins = wins + 1
            print('the computer used', y, '=> you won. Score', wins, "/", loses)

    elif x == 'paper':
        if y == 'rock':
            wins = wins + 1
            print('the computer used', y, '=> you won. Score', wins, "/", loses)
        elif y == 'paper':
            print('the computer used', y, '=> draw. Score', wins, "/", loses)
        elif y == 'scissors':
            loses = loses + 1
            print('the computer used', y, '=> you lost. Score', wins, "/", loses)

    elif x == 'scissors':
        if y == 'rock':
            loses = loses + 1
            print('the computer used', y, '=> you lost. Score', wins, "/", loses)
        elif y == 'paper':
            wins = wins + 1
            print('the computer used', y, '=> you won. Score', wins, "/", loses)
        elif y == 'scissors':
            print('the computer used', y, '=> draw. Score', wins, "/", loses)

    else:
        print(x, "is not a correct choice !")

请将其降低并提高到预期值。显示中间结果与预期结果的偏差。“未按预期工作”不是问题规范。您发布的代码挂起等待测试输入——提供测试输入是您的工作;不要期望我们每个人都创建并手动键入测试数据。您的x值是一个空字符串,因此不会执行if子句。您可以将行
input(x)
更改为
x=input()
Python需要适当的缩进。