You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求实现无input的Python猜数字程序(附尝试代码)

无input的猜数字Python程序实现求助

我有一段依赖input()的猜数字Python程序,现在想改成不依赖input的版本,打算用列表或变量来实现,但自己写的代码没成功,想请人帮忙完成。

原依赖input的代码

import random
number = random.randint(1, 10)

player_name = "doo"
number_of_guesses = 0
print('I\'m glad to meet you! {} \nLet\'s play a game with you, I will think a number between 1 and 10 then you will guess, alright? \nDon\'t forget! You have only 3 chances so guess:'.format(player_name))

while number_of_guesses < 3:
    guess = int(input())
    number_of_guesses += 1
    if guess < number:
        print('Your estimate is too low, go up a little!')
    if guess > number:
        print('Your estimate is too high, go down a bit!')
    if guess == number:
        break
if guess == number:
    print( 'Congratulations {}, you guessed the number in {} tries!'.format(player_name, number_of_guesses))
else:
    print('Close but no cigar, you couldn\'t guess the number. \nWell, the number was {}.'.format(number))

需求

  • 实现无input()的版本
  • 用列表或变量替代用户输入的部分

我尝试的代码(未成功)

import random
list=[1, 2, 3, 4, 5, 8, 9, 10]
print('I am Guessing a number between 1 and 10:\n')
for number in lis:
    number_of_guesses = 0
    while number_of_guesses<3:
        guess_number=random.randint(1,10)
        if number<guess_number:
            number_of_guesses+=1
            print('Your guess number is high '+str(guess_number))
        elif number>guess_number:
            number_of_guesses+=1
            print('Your guess number is low '+str(guess_number))
        else:
            print("You guess Right The number is: "+str(guess_number)+"\nNumber of guess taken "+str(number_of_guesses+1))
            break
    if number_of_guesses==3:
        print("Sorry your chances of guessing is over! You can not guess the number correct")

希望能帮忙修正这段代码,实现无input()的猜数字功能。

内容的提问来源于stack exchange,提问作者dodo87

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 02:30:44