我不知道错误是什么?

时间:2014-04-28 20:01:09

标签: python

我试图制作一个将狗年转换为人类年的转换器,反之亦然。 但是底部的if语句是,我不知道,不会完成代码。 我使用的是3.4。

print ("This program converts dog and cat years to human years and vice ser versa")

print("1. dog years to human years.")
print("2. Human years to dog years.")
print("3. cat years to human years.")
print("4. Human years to cat years.")

A = input("Choose one")

a = int(A)
if a == 1:
    B = input("How old is your dog")
    d = int(B)
    b = int(B)
    if b > 2:
        b = (d+2)
        Z = (b-d*1)
        Y = (d-2*4)
        print = (Y+Z)

2 个答案:

答案 0 :(得分:1)

您的问题显然是print = (Y+Z)行,但这并不是您的代码最大的问题。给我一点时间写点什么,我会尽力帮忙。

容易修复的最大问题是变量名称。什么是A?如何abBdZY ???这些是不可能的变量来处理。永远不要这样做。

根据经验,永远不要编写您认为您必须维护的代码。那有意义吗?也许不是......让我们用另一种方式......

如果StackOverflow的用户(或任何地方,或任何地方的任何人)查看您的代码并且去了#34;那到底是什么意思?"然后你写错了。这不是一种意见,而是编程中的真理。你写的一切,别人都要维持。如果他们需要深入挖掘代码以找出Z所代表的内容,他们就会永远诅咒你的名字。

而是确保您的变量具有描述性。 E.g:

user_in = input("Choose one: ") # <- A = input("Choose one: ")
user_in = int(user_in)          # <- a = int(A) ## why not reuse?
if user_in == 1: # hey that makes sense now!
    age = input("How old is your dog? ") # you can also do int(input("prompt"))
    age = int(age)
    if age > 2:
        dog_years = 2 + age - 8 # this is what your code evaluates to.
        print(dog_years)        # maybe that's a math error?

答案 1 :(得分:1)

我看到的问题:

  1. else:阻止后,您没有if b >2:阻止。

  2. 这些行似乎不正确。

    Z = (b-d*1)
    Y = (d-2*4)
    

    你可能意味着

    Z = (b-d)*1
    Y = (d-2)*4
    
  3. 非常明显:

    print = (Y+Z)
    

    需要:

    print (Y+Z)