请求实现无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
相关产品推荐
相关产品推荐

