提示错误‘>‘ not supported between instances of ‘module‘ and ‘int

import random
num = random.randint(1, 100)
print('%d'%num)
num = int(num)
guess_num = input('猜一个数字:')
guess_num = int(guess_num)

while random != guess_num:
    if random > guess_num:
        print('bigger')
        guess_num = input('猜一个数字:')
        guess_num = int(guess_num)
    else:
        print('bigger')
        guess_num = input('猜一个数字:')
        guess_num = int(guess_num)

print('猜对了是%d'%num)

这段代码实现了一个猜数字的游戏。但是它的逻辑有点错误。在 while 循环中,应该是比较 num 和 guess_num 是否相等,而不是和 random 是否相等。因为一开始随机生成的数字是被保存在 num 中,而不是在 random 中。

import random
num = random.randint(1, 100)
print('%d'%num)
num = int(num)
guess_num = input('猜一个数字:')
guess_num = int(guess_num)

while num != guess_num:
    if guess_num > num:
        print('bigger')
        guess_num = input('猜一个数字:')
        guess_num = int(guess_num)
    else:
        print('smaller')
        guess_num = input('猜一个数字:')
        guess_num = int(guess_num)

print('猜对了是%d'%num)```

你可能感兴趣的:(数学建模,python)