如何同时要求整数和字符串

时间:2016-01-06 19:09:12

标签: python string integer

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作为字符串我只是不知道如何在同一输入中使用字符串和整数操作(我要求输入,它既是字符串又是整数)。

由于

2 个答案:

答案 0 :(得分: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()返回一个字符串,raw_input尝试将输入作为Python表达式运行。

在python 3中,input只是重命名为{{1}},因此可以直接使用它作为字符串输入。

答案 1 :(得分: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()