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.

3
  • what error do you come across? Commented Feb 2, 2016 at 3:54
  • Take a look at this question for a start: stackoverflow.com/questions/5424716/… Commented Feb 2, 2016 at 4: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") Commented Feb 4, 2016 at 22:51

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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")
0

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

something = input()

try:
    something = int(something)
except:
    print("not an int")

2 Comments

Won't your if always return true? Because you check if number is an int right after casting it as an int.
thats what the user is doing
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!

4 Comments

wouldn't this throw a ValueError if user passes a string eg. "hello"
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")
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.