如何在python中制作一个猜谜游戏我正在努力

时间:2017-09-25 05:37:14

标签: python

这是我的代码

import random
print ('hello')
print ('my name is IB what is your name')
mn = input()
print ('it is good to meet you ' + mn+ ' I am thinking of a nomber 1 to 
100')
it = 1
jojm = random.randint (1, 100)
print ('guess')
guess =input()
while it < 100:
    if guess == jojm:
        print ('game over ')
        break
    elif guess < jojm: #it keeps saying the problem is here
        print ('to low')
        print ('guess')
    elif guess > jojm:
        print ('to high')
        print ('guess')
    it = it + 1
print =it

我不知道该怎么做请帮助我需要它,如果有人可以提供帮助请

4 个答案:

答案 0 :(得分:1)

您应该在循环中获取输入并将其解析为整数:

while it < 100:
    guess = int(input())
    #rest of your code

提示:您可以在输入法本身提供提示文字,例如input('Guess ?')

答案 1 :(得分:1)

guess =input()转换为guess =int(input())

因为guess =input()需要string而你正在与int进行比较。

在这里你可以试试这个。

jojm = random.randint (1, 100)
print('guess')
while True:
    guess = int(input())
    if guess == jojm:
        print ('game over ')
        break
    elif guess < jojm:
        print ('to low')
        print ('guess')
    elif guess > jojm:
        print ('to high')
        print ('guess')

答案 2 :(得分:1)

问题是guess是一个字符串,您需要将其转换为int:

import random
print ('hello')
print ('my name is IB what is your name')
mn = input()
print ('it is good to meet you %s I am thinking of a number between 1 and 
100' % mn)
it = 1
jojm = random.randint (1, 100)
print ('guess')
getGuess = input()
guess = int(getGuess) #convert it first to an integer 
while it < 100:
    if guess == jojm:
        print ('game over ')
    elif guess < jojm: 
        print ('to low')
        print ('guess')
    elif guess > jojm:    
        print ('guess')
        it = it + 1

print (it)

答案 3 :(得分:0)

把这一行

guess =input()

在while循环中 比每次猜测都是打印程序会要求你输入一个值。