2013-11-24 47 views
-2
#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") 

任何幫助將是一項很棒的服務。此外,我只編程了幾個星期,如果我的代碼看起來不對,很抱歉。

+0

你能修復了縮進嗎? – AHuman

+0

你在程序中實際輸入了什麼內容? – jazzpi

+0

要浮動的字符串?您可以在幾秒鐘內完全發現pydocs或谷歌。這個問題不是特別有用,嚴重缺乏研究。 –

回答

0
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() 
+0

它不適用於像59.5這樣的數字。當我將float添加到輸入時,它就會中斷。 – user3026270

+0

爲什麼在地球上你會玩浮點數的猜謎遊戲?它需要從「大約8」到「大約30」所需的猜測次數;這跟玩「猜數字在43億之間」是一樣的。 –

0

你可以通過定義,要求用戶輸入,直到浮子提供的功能分離的擔憂:

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) 
+0

我想添加揭示功能。如果我添加此功能,它將不再工作 – user3026270

+0

@ user3026270:我已經添加了對「揭示」的支持。 – jfs

+0

現在我得到如果猜測 user3026270

0

使用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 

這將試圖將猜測轉換爲一個浮點數,並且它失敗,那麼它將保持一個字符串。這應該可以解決你的問題。

+0

這實際上並沒有做任何事情。 ValueError:無法將字符串轉換爲float:'reveal' – user3026270

+0

然後嘗試使用random.range – AHuman

+0

當我使用secret_number = random.range(1,100,1)時我得到'module'對象沒有屬性'range' – user3026270

1

如果你想使用浮點數進行比較,遊戲可能是無止境的,因爲浮點數有許多小數位。使用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)' 
相關問題