規則如下:挑戰:編寫一個程序,從 1 到 100 中隨機選擇一個整數,然后讓玩家猜測這個數字。規則是:如果玩家的猜測小于 1 或大于 100,請說“OUT OF BOUNDS” 在玩家的第一個回合中,如果他們的猜測在數字的 10 以內,則返回“WARM!” 距離數字超過10,返回“COLD!” 在所有后續回合中,如果猜測比之前的猜測更接近數字,則返回“WARMER!” 比之前猜測的數字更遠,返回“COLDER!” 當玩家的猜測等于數字時,告訴他們他們猜對了,并猜了多少次!我嘗試在沒有指導的情況下僅使用我學到的基本工具來寫這篇文章。迄今為止。這是我的代碼(仍在處理中)guess=randint(0,100)## Guessing game !?# In this game, we will pick a random integer from the integers in the segment [0,100].?# In each step one should guess the integer that the system chose.# Once you gussed correctly, you will win the game.GuessList=[]Guess=input('Your Guess is: ')GuessList.append(Guess)if int(Guess)<0 or int(Guess)>100:? ? print('OUT OF BOUNDS')else:? ? if? int(Guess)==int(guess):? ? ? ? print('Congragulations, you have earned your chicken for friday \n Game is over now.')? ? else:? ? ? ? if 0<int(guess)-int(Guess)<10:? ? ? ? ? ? print('Warm')? ? ? ? elif 0<int(Guess)-int(guess)<10:? ? ? ? ? ? print('Warm')? ? ? ? else:? ? ? ? ? ? print('Cold')? ? ? ? NewGuess=input('Your new guess is: ')? ? ? ??? ? ? ??if int(NewGuess)==int(guess):? ? ?print('Congragulations, you have earned your chicken for friday \n The game is over now')? ? ? ? ? ? ? ??while int(NewGuess)!=int(guess):? ? if int(NewGuess)<0 or int(NewGuess)>100:? ? ? ? print('OUT OF BOUNDS')我確信那非常糟糕。我的問題是:首先,我知道我還沒有完成任務中寫的事情。我的代碼不會告訴猜測者他是否更接近正確的數字與他先前的猜測有關,而是與第一個猜測有關。不要告訴我如何正確編寫它,一旦我理解了更關鍵的問題,我就會弄清楚:正如它所寫的,無論我試圖猜測什么,它都永遠不會結束游戲(就好像 0-100 之間的所有數字)都是不正確的。另外,當我運行它并嘗試運行簡單的代碼時guess,它什么也不做(我正在木星筆記本中工作)。但是,如果我不運行此代碼,只是運行guess=randint(0,100)然后運行guess它確實會顯示數字。提前致謝。
我在運行 Python 任務時遇到問題
慕的地8271018
2023-08-08 16:50:27