2017-04-13 37 views
0

我對編碼有一個非常簡短的熟悉,儘管到目前爲止我的大部分經驗都與Python有關。這就是爲什麼它令人沮喪,我似乎無法弄清楚如何做到這一點......我可以使用什麼Python3方法將輸入限制在我的任務中唯一的整數?

我在我的Python類中有一個任務,我需要計算直角三角形的面積。我已經成功完成了任務,但我想進一步進一步限制用戶輸入除整數之外的任何內容作爲輸入。我已經嘗試了從我在Codecademy上學到的多個想法,儘管我似乎無法弄清楚。任何幫助將不勝感激!

這是我迄今爲止編寫的代碼;它工作得很好,它是什麼,但我想有它返回一個字符串,它類似於「請輸入一個有效的數字」如果用戶除了一些輸入任何東西:

from time import sleep 
import math 

print("Let\'s find the area of a right triangle!") 
sleep(2) 

triangleBase = float(input("Enter the base value for the triangle: ")) 
print("Great!") 

triangleHeight = float(input("Enter the height value for the triangle: ")) 
print("Great!") 
sleep(2) 

print("Calculating the area of your triangle...") 
sleep(2) 

def triangleEquation(): 
    areaString = str("The area of your triangle is: ") 
    triangleArea = float(((triangleBase * triangleHeight)/2)) 
    print('{}{}'.format(areaString, triangleArea)) 

triangleEquation() 
+0

我對這類東西一般不瞭解太多......因此需要上課。是否有另一種方法來捕捉用戶輸入,然後才能允許我尋找? – therealjayvi

+0

選中此項... [我如何限制用戶輸入爲Python中的整數](http://stackoverflow.com/questions/23326099/how-can-i-limit-the-user-input-to-only -pygers) – abccd

回答

1

你接近。你注意到你的代碼引發了一個異常。您只需要捕捉該異常並再次提示即可。本着「不要重複自己」的精神,這可以是它自己的功能。我清理了幾個其他的事情,比如你的計算功能使用全局變量和轉換的東西,不需要轉化(例如「富」是一個海峽,你不需要str('foo')),並得到了

from time import sleep 
import math 

def input_as(prompt, _type): 
    while True: 
     try: 
      # better to ask forgiveness... we just try to return the 
      # right stuff 
      return _type(input(prompt.strip())) 
     except ValueError: 
      print("Invalid. Lets try that again...") 

def triangleEquation(base, height): 
    area = base * height/2 
    print('The area of your triangle is: {}'.format(areaString, area)) 


print("Let\'s find the area of a right triangle!") 
sleep(2) 

triangleBase = input_as("Enter the base value for the triangle: ", float) 
print("Great!") 

triangleHeight = input_as("Enter the height value for the triangle: ", float) 
print("Great!") 
sleep(2) 

print("Calculating the area of your triangle...") 
sleep(2) 

triangleEquation(triangleBase, triangleHeight) 
+0

謝謝!這正是需要的!所以看來我需要熟悉如何使用Try和Except。這工作完美的需要:) – therealjayvi

+1

有一個理論在Python中稱爲[鴨子打字](https://docs.python.org/3.4/glossary.html#term-duck-typing),說你的代碼應該假設所有的事情都會在你面前爆發時,行動就會發揮作用並捕捉異常。 – tdelaney

0

這應該得到你開始了。我會把剩下的給你(如果你想讓花車,提示,提示),因爲你說你想更進一步,這樣你仍然會保持成就感。

def triangleEquation(triangleBase, triangleHeight): 
    if type(triangleHeight) == int and type(triangleBase) == int: 
     areaString = "The area of your triangle is: " 
     triangleArea = float(((triangleBase * triangleHeight)/2)) 
     return '{}{}'.format(areaString, triangleArea) 
    else: 
     return 'Integers only.' 

注意:您還可以使用is成語:if type(triangleHeight) is int ...

+1

我希望我有足夠的代表能夠upvote你的答案,你的答案真的幫助我開啓了我的眼睛,我現在一定會開始研究的新術語和概念! – therealjayvi

+0

謝謝。您希望將問題的解決方案更進一步,這是很好的。這是一個很好的習慣。我希望你喜歡用Python進行編程。我做!祝你好運!另外,請注意,我已將''triangleEquation()'函數的'print()'函數用於調用。這是另一個好習慣:讓你的函數「富有成效」(返回值不是'None',如果沒有返回值,這是默認值,並且被認爲是'void'函數。 'main()'函數 – GH05T

+0

這是我第一次開始使用Python的時候發現的一個資源,它僅僅是幾個月前;它是python.org上推薦的書籍之一:http://openbookproject.net/ thinkcs/python/english3e/fruitful_functions.html – GH05T

相關問題