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

2024-06-16 13:09:32 发布

您现在位置:Python中文网/ 问答频道 /正文

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

谢谢


Tags: andtoyouinputneedlengthintprint
2条回答

您可以使用如图所示的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()

在Python2.7中,您只需像

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表达式运行。你知道吗

在python3中,raw_input只是重命名为input,因此可以直接使用它来获取字符串形式的输入。你知道吗

相关问题 更多 >