Python猜数字游戏 [英] Python guess the number game

查看:57
本文介绍了Python猜数字游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 python 中猜数字游戏,但每当我猜它重复 4 次时,'你的猜测太低了'

I tried to make a guess the number game in python but whenever I guess it repeats 4 times 'your guess is too low'

import random
number = random.randint(1, 20)
guessestaken = 0
print('I am thinking of a number between 1 and 20 ')
guess = raw_input('Take a guess and hit enter')
while guessestaken < 4:
    guessestaken = guessestaken + 1
    if guess > number:
        print('your number is too low')
    if guess < number:
        print('your number is too high ')
    if guess == number:
        break
        print('well done the number was ' + number + ' and you got it in ' + guessestaken + '')

推荐答案

您在 while 循环之前要求用户输入.

You are asking for the user input before the while loop.

guess = int(raw_input('Take a guess and hit enter')) 

这个语句应该出现在 while 块中.

This statement should come within the while block.

函数 raw_input 返回一个字符串,你应该把它转换成一个整数.您可以在文档中阅读更多相关信息.

The function raw_input returns a string, you should convert it to an integer. You can read more about it in the Documentation.

这篇关于Python猜数字游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆