Python, unable to convert input() to int()

前端 未结 3 1969
遥遥无期
遥遥无期 2021-01-22 16:12

I am trying to convert input() data to int() with the following code:

prompt_text = \"Enter a number: \"
try:
  user_num = int(input(prompt_text))
except ValueEr         


        
3条回答
  •  隐瞒了意图╮
    2021-01-22 16:51

    This may not be the cleanest solution but it addresses the problem, in your code user_num is not initialized unless it is a number.

    prompt_text = "Enter a number: "
    user_num = "no Input"
    try:
      user_num = int(input(prompt_text))
    except ValueError:
      print("Error")
    
    if str(user_num).isnumeric():
      for i in range(1,10):
        print(i, " times ", user_num, " is ", i*user_num)
    
      even = ((user_num % 2) == 0)
    
      if even:
        print(user_num, " is even")
      else:
        print(user_num, " is odd")
    else:
      print("You did not enter a number")
    

提交回复
热议问题