2016-01-06 50 views
1
print("welcome to the Pythagoras calculator") 
print("please leave empty whenever asked for any sides of the triangle you do not have data for") 
print("please answer every question with integers only") 

side = input("which side would you like to be found?") 

hyp = int(input("hypotenuse length:")) 
if hyp == (''): 
    hyp = str(hyp) 
adj = int(input("adjacent length:")) 
if adj ==(''): 
    adj = str(adj) 
opp = int(input("opposite length:")) 
if opp == (''): 
    opp = str(opp) 

while hyp == ("") and adj == (""): 
    print("you need to insert two values") 
    hyp = int(input("hypotenuse length:")) 
    adj = int(input("adjacent length:")) 
    opp = int(input("opposite length:")) 

while hyp == ("") and opp == (""): 
      print("you need to insert two values") 
      hyp = int(input("hypotenuse length:")) 
      adj = int(input("adjacent length:")) 
      opp = int(input("opposite length:")) 

while adj == ("") and opp == (""): 
      print("you need to insert two values") 
      hyp = int(input("hypotenuse length:")) 
      adj = int(input("adjacent length:")) 
      opp = int(input("opposite length:")) 

while adj == ("") and opp == (""): 
      print("you need to insert two values") 
      hyp = int(input("hypotenuse length:")) 
      adj = int(input("adjacent length:")) 
      opp = int(input("opposite length:")) 

while hyp == ("") and adj == ("") and opp == (""): 
      print("you need to insert two values") 
      hyp = int(input("hypotenuse length:")) 
      adj = int(input("adjacent length:")) 
      opp = int(input("opposite length:")) 

我想創建一個畢達哥拉斯計算器,但當我要求人們插入邊的長度它彈出一個錯誤,說基本上我試圖使用int作爲字符串(在驗證中),我知道我不能使用int作爲字符串我只是不能弄清楚如何在同一個輸入中同時使用字符串和整數(我要求一個輸入,它既是一個字符串又是一個整數)。如何要求一個整數和一個字符串

感謝

+0

'如果HYP ==(「」)'永遠不會發生,如果字符串是空的,你會得到一個錯誤,如果你想驗證輸入使用try /除外。 http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response –

+1

寫你自己的輸入函數,檢查用戶輸入的字符串的長度和使用try/except子句來查看它是否可以使用int()來轉換爲整數,然後用它代替int(input(「name:」))'。 – martineau

回答

0

在Python 2.7版,您只需使用raw_input()

hyp = raw_input("hypotenuse length:") 
adj = raw_input("adjacent length:") 
opp = raw_input("opposite length:") 

然後支票將工作,如果不輸入任何他們將空字符串。

你也最好使用raw_input對所有輸入,無論你將需要價值爲int你明確地隱蔽它,然後,像

temp = int(adj) 

raw_input()返回一個字符串,並input()嘗試運行輸入作爲Python表達式。

在python 3中,raw_input只是更名爲input,因此可以直接使用輸入字符串。

+0

非常感謝! –

+0

我希望它明確指出,將這些冗餘檢查轉換爲字符串的需求將不再需要。因此,您的程序將立即轉到那些while循環中的檢查。 – wolfsgang

0

你可以使用一個try/except塊,如圖所示,添加附加條件,以驗證(我在validate_input()檢查的空白麪的數量,但你可以擴展到正數,等等)。

#!/usr/bin/python 

import math 

#Triangle has three sides; two can be defined and the third is calculated 
class Triangle: 

    def __init__(self): 
     self.side={"adjacent":0,"opposite":0,"hypotenuse":0} 

    def define_sides(self): 
     for i in self.side: 
      self.side[i]=self.get_side(i) 

    def print_sides(self): 
     for i in self.side: 
      print "side",i,"equals",self.side[i] 

    #return user integer or None if they enter nothing 
    def get_side(self,one_side): 
     prompt = "Enter length of "+one_side+": " 
     try: 
      return input(prompt) 
     except SyntaxError: 
      return None 

    def count_nones(self): 
     nones=0 
     for side, length in self.side.iteritems(): 
      if self.side[side] == None: 
       nones+=1 
     return nones 

    def validate_input(self): 
     nNones=self.count_nones() 

     if nNones < 1: 
      print "You must leave one side blank." 
      quit() 
     if nNones > 1: 
      print "Only one side can be left blank." 

    def calculate_missing_length(self): 
     h=self.side["hypotenuse"] 
     a=self.side["adjacent"] 
     o=self.side["opposite"] 

     #hypotenuse is missing 
     if h == None: 
      self.side["hypotenuse"] = math.sqrt(a*a+o*o) 

     #adjacent is missing 
     if a == None: 
      self.side["adjacent"] = math.sqrt(h*h-o*o) 

     #opposite is missing 
     if o == None: 
      self.side["opposite"] = math.sqrt(h*h-a*a) 

#begin program 
triangle=Triangle() 
triangle.define_sides() 
triangle.print_sides() 
triangle.validate_input() 
triangle.calculate_missing_length() 
triangle.print_sides() 
相關問題