我正在写一个燃料转换程序,它不工作:(

时间:2016-05-04 01:13:03

标签: python

我是一个新手python代码编写器,我从燃料转换程序开始很小。程序会询问您的姓名,然后转换每加仑英里数或每升公里数。目前,该程序运行良好,直到它转换为MPG线。然后一旦你按下y,它什么都不做。有趣的是,没有返回语法错误。请帮忙,因为我找不到任何东西:(

import time

y = 'y', 'yes', 'yep', 'yea', 'ye'
n = 'n', 'no', 'nup', 'nay'

name = str(input("Hey, User, whats your name?   "))

time.sleep(1.5)

print("Alright", name, "Welcome the the *gravynet* Fuel Efficiency Converter!")

time.sleep(1.5)

str(input("Would you like to convert the fuel efficiency of your motor vehcile? (Miles Per Gallon) (y/n):    "))

if y is True:

miles = int(input("How far did you travel (in miles):   "))

galls = int(input("How much fuel did you consume (in gallons):   "))

 mpgc = (galls/miles)

 print("The MPG Rate is:  ", int(mpgc))

 time.sleep(2)

 print("test print")
if y is (not True):

  input(str("Would you like to convert KPL instead? (y/n):   "))

  time.sleep(1.5)

  if y is True:

   kilometers = int(input("How far did you travel (in kilometers):   "))

   litres = int(input("How much fuel did you consume (in litres):   "))

   kplc = ( litres / kilometers )

   print("The KPL Rate is:   ", int(kplc))

   time.sleep(3)

   exit()


  if y is not True:

   print("No worries")

   time.sleep(1.5)

   print("Thanks", name, "for using *gravynet* Fuel Efficiency Coverter")

   time.sleep(1.5)

   print("Have a good day!")

   time.sleep(1.5)

   exit()

  else :
     print("Sorry, invalid response. Try again")
     exit()


elif not y:

  print("Please use y/n to answer" )

  time.sleep(2)

elif not n:

  print("Please use y/n to answer" )

  time.sleep(2)

抱歉,如果您认为这很糟糕,但我刚开始使用python,我需要一些帮助:)

3 个答案:

答案 0 :(得分:1)

严格修剪和缩进固定(我认为....)

if y is True和类似if y is not True在这里毫无意义。

另外,提及is .. is==有时可用作检查"相等"的等效表达式,但不一定如此。 ==检查是否相等,而is检查对象标识。您应该使用==来检查两个对象之间的相等性。除了None之外,在这种情况下,通常首选使用is代替==

您不必要地在一堆地方转换为str。他们已经是字符串了。

在你的mpg转换中你已经有一个浮点数(可能是一个int)。这里没有必要转换为int。假设mpg是< 1.然后int施法将使此返回零

你的数学也是倒退的。里程加仑。同样,公里加仑。

name = input("Hey, User, whats your name?   ")
print("Alright", name, "Welcome the the *gravynet* Fuel Efficiency Converter!")
mpg = input("Would you like to convert the fuel efficiency of your motor vehcile? (Miles Per Gallon) (y/n): ")

if mpg in y:

    miles = int(input("How far did you travel (in miles):   "))
    galls = int(input("How much fuel did you consume (in gallons):   "))

    mpgc = miles / galls

    print("The MPG Rate is:  ", mpgc)

else:
    kpl = input("Would you like to convert KPL instead? (y/n):   ")

    if kpl in y:
        kilometers = int(input("How far did you travel (in kilometers):   "))
        litres = int(input("How much fuel did you consume (in litres):   "))
        kplc = kilometers / litres
        print("The KPL Rate is:   ", kplc)

    else:

        print("No worries")
        print("Thanks", name, "for using *gravynet* Fuel Efficiency Coverter")
        print("Have a good day!")

答案 1 :(得分:0)

python中的is关键字检查两个变量是否指向内存中的相同位置。 y永远不会指向与单例True相同的内存位置,因为它的值是一个字符串。我怀疑你的意思是

inp = str(input("Would you like to convert the fuel efficiency of your motor vehcile? (Miles Per Gallon) (y/n):    "))
if inp in y:
    ...

答案 2 :(得分:0)

你不能直接从键盘上按下y,你必须把它作为输入(按需输入),存储它,检查它是否满足条件,然后应用逻辑。 / p>

我看到你试图将y和n定义为一个元组(有意或无意),在这种情况下,我假设你也想把其他单词当作yesor

在这种情况下,您可以应用此逻辑;

inp = input("Would you like to convert the fuel efficiency of your motor vehcile? (Miles Per Gallon) (y/n):    ")
if inp in y: # Check if inp corresponds any of the words defined in y
    # Things to do if `yes` or anything similar entered.

一些注意事项:

  • 如果您正在使用输入,则无需使用str() Python3(看起来你是这样)。因为input()返回字符串。

  • 在某个地方你做过这样的事情:

    input(str("Would you like to convert KPL instead? (y/n): "))

    由于您输入的值已经存在,因此更具有还原性 一个字符串。

  • 您也没有为整个过程中的任何变量分配一些输入 码。您应该分配它们如果您以后要使用它们。

请注意这些问题。