无法获取代码来检索Input()的指数,并且无法确定是数字还是字母来输出适当的注释

时间:2019-05-23 23:07:36

标签: python python-3.x

更新:我发现'break'不适合在这里使用,而应该使用'sysexit()'。

Value = input("Put a number in here, watch it multiply itself! [q to quit]: ")
try:
    Value == 'q'
    sysexit()
except Value == ValueError:
    print("Excuse me; I don't think that's a number. Please try again!")
except request == int(Value):
    print(value + " multipled by " + value + "is " (value * value))

现在加载了代码,但是它不接受任何输入,给了我两个错误:

Traceback (most recent call last):
File "More if then practice", line 4, in <module>
sysexit()
NameError: name 'sysexit' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "More if then practice", line 5, in <module>
    except Value == ValueError:
TypeError: catching classes that do not inherit from BaseException is not allowed

***Repl Closed***

但是仍然不确定该怎么做。 我正在使用o'reilly的系列书籍学习Python,并且有一个演示如何使用“ While True”循环来检查input()变量

while True: 
    value = input(" Integer, please [q to quit]: ") 
    if value = = 'q':     # quit function 
       break
    number = int( value)
  if number % 2 = = 0: # an even number
    continue 
print( number, "squared is", number* number)

(BillLubanovic。Python简介-O'Reilly Media,Inc。)

出于锻炼目的,我一直在尝试使此代码具有以下功能:

  

戒烟能力

     

检测输入的是数字还是字母/其他

     

将奇数和偶数相乘。

我有一个昨晚编写的版本,该版本至少可以运行,但是不会乘法,今天早晨,我发现了一个StackOverflow帖子,其中为“ try / except”参数提供了两个例外。现在我的代码如下:

Value = input("Put a number in here, watch it multiply itself! [q to quit]: ")
try:
    Value == 'q'
    break
except ValueError:
    print("Excuse me; I don't think that's a number. Please try again!")
except request == int(Value):

print(value + " multipled by " + value + "is " (value * value))

但是我不断收到以下错误:

line 4
break
^
SyntaxError: 'break' outside loop

我已经转移了代码,改用了一段时间的true / for循环,并仔细检查了互联网上其他帖子的语法,并相信我在这里正确使用了它。

Value = input("Put a number in here, watch it multiply itself! [q to quit]: ")
try:
    Value == 'q'
    break
except ValueError:
    print("Excuse me; I don't think that's a number. Please try again!")
except request == int(Value):

print(value + " multipled by " + value + "is " (value * value))

但是我不断收到以下错误:

line 4
break
^
SyntaxError: 'break' outside loop

预期:可以执行到我可以满足初始摘要中列出的标准的地方

实际:由于“摘要”和“代码”部分中列出的错误而崩溃。

1 个答案:

答案 0 :(得分:0)

从原始代码开始:

while True: 
    value = input(" Integer, please [q to quit]: ") 
    if value = = 'q':     # quit function 
       break
    number = int( value)
  if number % 2 = = 0: # an even number
    continue 
print( number, "squared is", number* number)

错误来源可能包括

  • 缩进已关闭(在python中很重要)
    • 您的if语句必须处于同一级别
    • 您的break需要另一个空间
    • 您的print()需要在循环内缩进
  • = =必须为==(没有空格,因为这会导致SyntaxError
  • 不进行错误检查(我相信您的意图是“检测输入的内容是数字还是字母/其他”)

包括您的要求:

  

将奇数和偶数相乘。

只需取出第二条if语句。

  

戒烟能力

只需修复= =-> ==

  

检测输入的是数字还是字母/其他

尝试将输入强制转换为int(),如果它抛出ValueError异常,则为“其他”

将它们放在一起:

while True:
    value = input("Please enter an integer (q to quit):\n>>>") # I like aesthetic prompts
    if value == 'q': # quit function
        break
    try:
        number = int(value)
    except ValueError:
        print("That's not a valid number")
        continue
    print("{} squared is {}".format(number, number ** 2))

带有示例输出:

Please enter an integer (q to quit):
>>>5
5 squared is 25
Please enter an integer (q to quit):
>>>16
16 squared is 256
Please enter an integer (q to quit):
>>>stop
That's not a valid number
Please enter an integer (q to quit):
>>>6
6 squared is 36
Please enter an integer (q to quit):
>>>q

Process finished with exit code 0

希望这会有所帮助,欢迎使用StackOverflow!