【问题标题】:Having trouble with a beginner python program that I tried - why is this not working?我尝试过的初学者 python 程序遇到问题 - 为什么这不起作用?
【发布时间】:2016-05-17 10:36:13
【问题描述】:

我在代码的最后一个 else 中得到一个错误,但我知道除此之外它可能还有其他问题。我会很感激我能得到的任何帮助。谢谢:)

carryon = True
while carryon == True:
   side1 = int(input("Please enter the smallest side of the triangle."))
   side2 = int(input("Please enter the second smallest side of the triangle."))
   side3 = int(input("Please enter the hypotenuse of the triangle."))
   if (side1 ** 2) + (side2 ** 2) == (side3 ** 2):
        print "Your triangle is a triple!"
    user = input("Would you like to continue? Type y to continue entering side lengths, type n to stop.")
    user.lower()
     if user == 'y':
      continue
     elif user == 'n':
      carryon = False
     else: 
      print "Please type either y or n!"
  else: 
    print "Your triangle is not a triple!"

【问题讨论】:

  • 您收到的错误信息是什么?
  • @Dave2e 它说最后 else 的语法无效。

标签: python-2.7


【解决方案1】:

我相信你有几个错误。关于第二个 else 所在位置的逻辑错误,潜在的缩进错误以及我的 Python 2.7。 Python 在将值从输入转换为字符串时遇到问题,我最好使用 input_raw() 输入字符串值。

carryon = True
while carryon == True:
    side1 = int(input("Please enter the smallest side of the triangle."))
    side2 = int(input("Please enter the second smallest side of the triangle."))
    side3 = int(input("Please enter the hypotenuse of the triangle."))
    if (side1 ** 2) + (side2 ** 2) == (side3 ** 2):
          print "Your triangle is a triple!"
    else: 
         print "Your triangle is not a triple!"

    user = raw_input("Would you like to continue? Type y to continue entering side lengths, type n to stop.")
    user.lower()
    if user == "y":
        continue
    elif user == 'n':
        carryon = False
    else: 
        print "Please type either y or n!"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 2021-05-13
    相关资源
    最近更新 更多