0

I was hoping someone could help or hint to where I'm going wrong with this Python homework assignment:

number = int(input("Enter a number"))
if number == int or float:
    print(number * number, number * number * number, number ** 4)
elif number != int or float:
    print("This is not a valid number")

It runs fine with a whole number, but not with a float or a string. I think it's because number is set to look for an integer, but I'm not sure what to substitute that with in order to make it work.

peak
  • 105,803
  • 17
  • 152
  • 177
Dylan
  • 45
  • 5
  • what error do you come across? –  Feb 02 '16 at 03:54
  • Take a look at this question for a start: http://stackoverflow.com/questions/5424716/python-how-to-check-if-input-is-a-number – Knells Feb 02 '16 at 04:01
  • FInal answer: number = input("Enter a number") try: number = int(number) print(number * number, number * number * number, number ** 4) except ValueError: try: number = float(number) print(number * number, number * number * number, number ** 4) except ValueError: print("This isn't an integer or float") – Dylan Feb 04 '16 at 22:51

3 Answers3

1

You want to use a try... except... else block:

try:
    number = float(input("Enter a number"))
except ValueError:
    print("This is not a valid number")
else:
    print(number * number, number * number * number, number ** 4)
pzp
  • 6,249
  • 1
  • 26
  • 38
  • I wound up extrapolating on this to get my final answer, so thanks pzp. This is what my final code looks like: number = input("Enter a number") try: number = int(number) print(number * number, number * number * number, number ** 4) except ValueError: try: number = float(number) print(number * number, number * number * number, number ** 4) except ValueError: print("This isn't an integer or float") – Dylan Feb 04 '16 at 22:50
0

you can wrap around input around a try ... except.

something = input()

try:
    something = int(something)
except:
    print("not an int")
GIRISH RAMNANI
  • 614
  • 6
  • 18
0

Can you just replace the int with a float? like this: "number = float(input("Enter a number"))"

I think it solves your problem. Anyway we could use a little more description.

Good luck!

  • wouldn't this throw a `ValueError` if user passes a string eg. "hello" – GIRISH RAMNANI Feb 02 '16 at 04:09
  • Yeah i didn't get it. He wants to do what python already does? Because python throws a "not a number" error when a string is passed.. If so i think try... except would be the solution Anyway, here's a solution with try... except try: number = float(input("Enter a number")) print(number * number, number * number * number, number ** 4) except: print("This is not a valid number") – Fernando Vaz Feb 02 '16 at 04:15
  • maybe he wants to like sanitize the user's input. like if the user sends something other than a int or a float then the program shouldnt break and rather show an error and ask again. – GIRISH RAMNANI Feb 02 '16 at 04:18
  • yes, i'm struggling with the markdown editing here I made a code that catches the error: try: number = float(input("Enter a number")) print(number * number, number * number * number, number ** 4) except: print("This is not a valid number") I hope you can read this. – Fernando Vaz Feb 02 '16 at 04:24