#My code should take a random between 1 and 100 and let you guess it.
#This part works, but I want to add the posibility to reveal the number and then is when I get the error "could not convert string to float"
def reveal(guess):
return secret_number
import random
secret_number = random.random()*100
guess = float(input("Take a guess: ")) #This is the input
while secret_number != guess :
if guess < secret_number:
print("Higher...")
elif guess > secret_number:
print("Lower...")
guess = float(input("Take a guess: ")) #This input is here in order for the program not to print Higher or Lower without ever stopping
else:
print("\nYou guessed it! The number was " ,secret_number)
if guess == "reveal": #This is where I "tried" to make the reveal thingy.
print ("Number was", secret_number)
input("\n\n Press the enter key to exit")
回答
import random
LOWEST = 1
HIGHEST = 100
def main():
print('Guess the secret number between {} and {}!'.format(LOWEST, HIGHEST))
secret = random.randint(LOWEST, HIGHEST)
tries = 0
while True:
guess = raw_input('Your guess: ').strip().lower()
if guess.isdigit():
tries += 1
guess = int(guess)
if guess < secret:
print('Higher!')
elif guess > secret:
print('Lower!')
else:
print('You got it in {} tries!'.format(tries))
break
elif guess == "reveal":
print('The secret number was {}'.format(secret))
break
else:
print('Please enter a number between {} and {}'.format(LOWEST, HIGHEST))
if __name__=="__main__":
main()
它不適用於像59.5這樣的數字。當我將float添加到輸入時,它就會中斷。 – user3026270
爲什麼在地球上你會玩浮點數的猜謎遊戲?它需要從「大約8」到「大約30」所需的猜測次數;這跟玩「猜數字在43億之間」是一樣的。 –
你可以通過定義,要求用戶輸入,直到浮子提供的功能分離的擔憂:
def input_float(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("You should input a float number. Try again.")
然後,你可以用它在你的腳本:
guess = input_float("Take a guess: ")
如果你想除了浮點數外,還接受'reveal'
作爲輸入:
def input_float_or_command(prompt, command='reveal'):
while True:
s = input(prompt)
if s == command:
return s
try:
return float(s)
except ValueError:
print("You should input a float number or %r. Try again." % command)
我想添加揭示功能。如果我添加此功能,它將不再工作 – user3026270
@ user3026270:我已經添加了對「揭示」的支持。 – jfs
現在我得到如果猜測
使用random.range random.random。
secret_number = random.range(1,100,1)
而且...,str(secret_number)
這樣,你將被串聯一個字符串的字符串。此外,你可以保持random.random只做第二次改變。
編輯:
另一件事要做的就是用raw_input
而不是input
。然後使用try
。
guess = raw_input("Take a guess: ")
try:
guess = float(guess)
except:
pass
這將試圖將猜測轉換爲一個浮點數,並且它失敗,那麼它將保持一個字符串。這應該可以解決你的問題。
這實際上並沒有做任何事情。 ValueError:無法將字符串轉換爲float:'reveal' – user3026270
然後嘗試使用random.range – AHuman
當我使用secret_number = random.range(1,100,1)時我得到'module'對象沒有屬性'range' – user3026270
如果你想使用浮點數進行比較,遊戲可能是無止境的,因爲浮點數有許多小數位。使用int數字。
#!/usr/bin/env python3.3
# coding: utf-8
import random
def guess_number():
try:
guess = int(input("Take a guess:"))
except ValueError:
print("Sorry, you should input a number")
guess = -1
return guess
if __name__ == '__main__':
secret_number = int(random.random() * 100)
while True:
guess = guess_number()
if guess == -1:
continue
elif guess < secret_number:
print("Lower...")
elif guess > secret_number:
print("Higher...")
else:
print("\nYou got it! The number was ", secret_number)
input("\n\nPress any key to exit.")
break # or 'import sys; sys.exit(0)'
- 1. C將字符串/字符輸入轉換爲浮點數
- 2. ValueError:無法將字符串轉換爲浮點數:無聊
- 3. 將字符串轉換爲浮點數
- 4. 將字符串轉換爲浮點數
- 5. 將字符串轉換爲浮點數?
- 6. 將字符串轉換爲浮點Java
- 7. 將字符串轉換爲浮點數
- 8. VBS將字符串轉換爲浮點
- 9. 將字符串轉換爲浮點數
- 10. ValueError:無法將字符串轉換爲浮點數:'[231.49377550490459]'
- 11. Python。 ValueError無法將字符串轉換爲浮點型:
- 12. 無法將字符串轉換爲浮點數
- 13. 無法將字符串值轉換爲浮點數
- 14. Python - ValueError:無法將字符串轉換爲浮點數:[9000]
- 15. ValueError:無法將字符串轉換爲浮點數:E-3
- 16. 無法將字符串轉換爲浮點型PHP - Laravel 5.2
- 17. 無法將字符串轉換爲浮點數:price [0] - python
- 18. ValueError無法將字符串轉換爲浮點數:[0.0,0.0,0.0,0.0,
- 19. ValueError:無法將字符串轉換爲浮點數:
- 20. numpy.loadtxt「無法將字符串轉換爲浮點數」
- 21. ValueError:無法將字符串轉換爲浮點數:「nbformat」:4
- 22. ValueError:無法將字符串轉換爲浮點數,(numpy.string_)
- 23. matplotlib.pyplot.plot,ValueError:無法將字符串轉換爲浮點數:f
- 24. 無法將字符串轉換爲浮點數php
- 25. Yahoo API ValueError:無法將字符串轉換爲浮點數:
- 26. 無法將列表中的字符串轉換爲浮點數
- 27. Python - ValueError:無法將字符串轉換爲浮點數:
- 28. 無法將字符串轉換爲浮點數:...有時
- 29. ValueError:無法將字符串轉換爲浮點數python
- 30. ValueError:無法將字符串轉換爲浮點數:Numpy數組
你能修復了縮進嗎? – AHuman
你在程序中實際輸入了什麼內容? – jazzpi
要浮動的字符串?您可以在幾秒鐘內完全發現pydocs或谷歌。這個問題不是特別有用,嚴重缺乏研究。 –