python random.randint未定义_学习python - 无法弄清楚random.randint
那么這是我根據您發布的問題文本做的快速重寫:
import random
input("Think of a number from 1 to 10, then press enter. Don't tell me, I'll guess it.")
low = 1
high = 10
done = False
for guesses in range(7):
number = random.randint(low, high)
print("I'm guessing", number)
happy = False
while not happy:
resp = input('Enter H if your number is higher, L if lower or E for equal: ')[0].upper()
if resp == 'E':
if guesses:
plural = 'es'
else:
plural = ''
print('Yay! I got it in {} guess{}.'.format(guesses+1, plural))
happy = True
done = True
break
elif resp == 'H':
happy = True
low = number + 1
elif resp == 'L':
happy = True
high = number -1
else:
print("That wasn't a valid response.")
if done:
break
print('end')我做了不同的事情:
該程序沒有將您的號碼作為輸入(它畢竟是在試圖猜測它)。
我設置低和&高到初始范圍并設置完成標志,因為我需要在成功時突破兩個循環級別
接下來我想基于低 - 高范圍。
要求H,L或E表示我的猜測落在哪里
根據用戶告訴我的內容修改范圍
主要區別在于這段代碼有點智能地猜測了數字。 (雖然有7個猜測它仍然可能失敗。)一個更智能的算法是每次猜測中點而不是隨機。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python random.randint未定义_学习python - 无法弄清楚random.randint的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 雷军晒3亿估值,意欲“收编”台湾硬件创业
- 下一篇: 会议论文_InVisor会议论文辅导发表