我在python中编码的新手,只是搞乱了,我想知道为什么我的代码只是将分数添加到机器人而不是我。我知道这要求很多,但请解释为什么它不起作用
from random import *
import random
from random import randrange
HumanPoints = 0
botPoints = 0
while True:
print('Im thinking of a number between one and two')
print('first to 5 points wins')
randomNum = randint(1, 2)
answer = input()
if answer == randomNum:
HumanPoints = HumanPoints + 1
print('You have {} points'.format(HumanPoints))
print('I have {} points'.format(botPoints))
if answer != randomNum:
botPoints = botPoints + 1
print('You have {} points'.format(HumanPoints))
print('I have {} points'.format(botPoints))
if HumanPoints >= 1:
break
if botPoints >= 1:
break
while True:
print('ok choose 1 or 2')
randomNum = randint(1, 2)
answer = input()
if answer == randomNum:
HumanPoints = HumanPoints + 1
print('You have {} points'.format(HumanPoints))
print('I have {} points'.format(botPoints))
if answer != randomNum:
botPoints = botPoints + 1
print('You have {} points'.format(HumanPoints))
print('I have {} points'.format(botPoints))
if HumanPoints >= 5:
print('you win')
break
if botPoints >= 5:
print('you lose')
break
答案 0 :(得分:0)
answer
以字符串形式提供用户输入,而不是数字。如果用户输入1,则"1"
将为1
,而不是"1" != 1
。
用户永远不会获得积分,因为"2" != 2
和answer = input()
。
将answer = int(input())
更改为escape('uuidgen | tr -d ''\n'' | awk ''{printf("\"%s\"", $0);}'')','\\/.*$^~[]')
。
答案 1 :(得分:0)
输入是一个字符串(" 1"或" 2")。字符串永远不能等于数字。试试这个:
answer = int(input())
这将使答案等于玩家输入的整数。