Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基本python编码_Python - Fatal编程技术网

基本python编码

基本python编码,python,Python,尝试用python编写一个简单的猜谜游戏程序,但我更喜欢java。 当输入正确的数字时,它表示该数字太高,不会退出while循环。 有什么建议吗 import random comp_num = random.randint(1,101) print comp_num players_guess = raw_input("Guess a number between 1 and 100: ") while players_guess != comp_num: if players_gue

尝试用python编写一个简单的猜谜游戏程序,但我更喜欢java。 当输入正确的数字时,它表示该数字太高,不会退出while循环。 有什么建议吗

import random
comp_num = random.randint(1,101)
print comp_num
players_guess = raw_input("Guess a number between 1 and 100: ")
while players_guess != comp_num:
    if players_guess > comp_num:
        print "Your guess is too high!"
    elif players_guess < comp_num:
        print "Your guess is too low!"
    players_guess = raw_input("Guess another number between 1 and 100: ")
print "CONGRATULATIONS! YOU GUESSED CORRECTLY!"
随机导入
comp_num=random.randint(1101)
打印组件编号
玩家猜=原始输入(“猜一个介于1和100之间的数字:”)
而你猜呢!=公司数量:
如果玩家猜猜>comp\u num:
打印“你的猜测太高了!”
elif玩家猜测
我猜这是因为您正在比较
string
int
。从
原始输入捕获的任何内容都将捕获为
字符串,在Python中:

print“1”>100#将打印为真
要使其工作,请转换:

players_guess = raw_input("Guess a number between 1 and 100: ")


您正在将字符串与int进行比较。这就是为什么会得到奇怪的结果

试试这个:

随机导入
comp_num=random.randint(1101)
打印组件编号
玩家猜=int(原始输入(“猜一个介于1和100之间的数字:”)
而你猜呢!=公司数量:
如果玩家猜猜>comp\u num:
打印“你的猜测太高了!”
elif玩家猜测
您需要强制输入int

请尝试以下代码:

import random
comp_num = random.randint(1,101)
print comp_num
players_guess = int(raw_input("Guess a number between 1 and 100: "))
while players_guess != comp_num:
    if players_guess > comp_num:
        print "Your guess is too high!"
    elif players_guess < comp_num:
        print "Your guess is too low!"
    players_guess = int(raw_input("Guess another number between 1 and 100: "))
print "CONGRATULATIONS! YOU GUESSED CORRECTLY!"
随机导入
comp_num=random.randint(1101)
打印组件编号
玩家猜=int(原始输入(“猜一个介于1和100之间的数字:”)
而你猜呢!=公司数量:
如果玩家猜猜>comp\u num:
打印“你的猜测太高了!”
elif玩家猜测
对于第二个
if
,使用
elif
…仍然只是输出太高…不断更新问题以反映答案(即使答案正确)不是一个好主意。它使答案看起来无关紧要,并消除了其他人从错误中学习的机会。节省在线提问的时间。谢谢大家!但是,一旦我输入正确的数字,它仍然不会退出while循环。或者您忘记将第二个
原始输入转换为
int
。。。就像你更新的问题所表明的那样。你不应该用答案更新你的问题,因为你只会把事情弄糊涂。@MaxRegitnig像maksimov说的那样,你可能忘记了在循环的底部投射第二个
raw_input
。或者干脆把100作为字符串欢迎来到动态打字的奇妙世界。你将学会爱它就像恨它一样。你错过了第二次原始输入调用中的int。是的,这是一个打字错误,向下投票并不是一个值得解决的问题-我没有向下投票,我只是陈述了一个可能有人这样做的原因-对我来说确实有点苛刻。你可以通过添加一个简短的解释来改进你的答案。
players_guess = int(raw_input("Guess a number between 1 and 100: "))
import random
comp_num = random.randint(1,101)
print comp_num
players_guess = int(raw_input("Guess a number between 1 and 100: "))
while players_guess != comp_num:
    if players_guess > comp_num:
        print "Your guess is too high!"
    elif players_guess < comp_num:
        print "Your guess is too low!"
   players_guess = int(raw_input("Guess another number between 1 and 100: "))
print "CONGRATULATIONS! YOU GUESSED CORRECTLY!"
import random
comp_num = random.randint(1,101)
print comp_num
players_guess = int(raw_input("Guess a number between 1 and 100: "))
while players_guess != comp_num:
    if players_guess > comp_num:
        print "Your guess is too high!"
    elif players_guess < comp_num:
        print "Your guess is too low!"
    players_guess = int(raw_input("Guess another number between 1 and 100: "))
print "CONGRATULATIONS! YOU GUESSED CORRECTLY!"