只为机器人团队加分

2024-09-28 20:52:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我对用python编写代码是全新的,只是在胡闹,我想知道为什么我的代码只是在给bot添加分数,而不是我。我知道这是要求很多,但请解释为什么它不工作

 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

Tags: 代码answerfromimportyouformatifhave
2条回答

input()将用户输入作为字符串,而不是数字。如果用户键入1,则answer将是"1",而不是1

用户从不得分,因为"1" != 1"2" != 2

answer = input()更改为answer = int(input())

输入是一个字符串(“1”或“2”)。字符串永远不能等于数字。试试这个:

answer = int(input())

这将使答案等于玩家键入的整数

相关问题 更多 >