While循环猜测数字游戏

编程入门 行业动态 更新时间:2024-10-11 01:16:08
本文介绍了While循环猜测数字游戏-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试进行猜测1-10之间的数字"游戏,但是while循环似乎一直在运行.我想编程让用户猜一个数字,然后显示它的值是否太高或太低等,然后自动重新开始(循环)以允许用户再次选择.此代码使它永远运行. 你们可以帮我吗?

I'm trying to make a 'guess the number between 1-10' game but the while loops seems to keep running. I want to program to let the user guess a number then display if its too high or low etc then start again automatically (loop) to allow the user to pick again. This code makes it run forever though. Can you guys help me?

import random def numberGuess(): printNow("I'm thinking of a number between 1 and 10") guess = 0 # give guess a starting value randNum = random.randrange(1,11) # this line generates a random number guess = int(input("Try to guess the number:")) # ask user for a number print randNum while guess != randNum: if (guess == randNum): print "You got it!" if (guess > randNum): print "Wrong! You guessed too high" if (guess < randNum): print "Wrong! You guessed too low"

推荐答案

您忘了在循环内猜测

while guess != randNum: guess = int(input("Try to guess the number:")) if (guess > randNum): print "Wrong! You guessed too high" if (guess < randNum): print "Wrong! You guessed too low" print "You got it!"

更多推荐

While循环猜测数字游戏

本文发布于:2023-11-30 13:18:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!