Feb-20-2018, 06:50 PM
Hi! I've been assigned a programming task to make around 10 or so different functions for squaring a number. I haven't been informed of any limitations so I felt this query needn't be in the Homework forums.
This here is where I'm at in the assignment (part C):
This here is my latest attempts (all of my other versions were similar and had the same problems that I'll go through below):
I don't know what's going on with this, because what I had hoped to happen was the function would print out the square of the input (if the input was an integer or floating point number) or print ("You did not input a number. Please try again." (if the input was anything else).
Hopefully this problem shouldn't be too difficult for programming experts such as yourselves! I would be very grateful for your help!
This here is where I'm at in the assignment (part C):
- a. Assuming knowledge of what a variable is; write a simple program that asks for a number and outputs the square of that number. Save it as Square_1.
- b. Change the previous program to use branching to decide whether the input is valid, i.e. is it a number? Save it as Square_2.
- c. Change the program to use iteration to allow more than one attempt at entering a number. Save it as Square_3.
This here is my latest attempts (all of my other versions were similar and had the same problems that I'll go through below):
def Square_3(x): for i in range(x): try: y = input("Please input a number: ") if int(y) == y or float(y) == y: print(y**2) except: print("You did not input a number. Please try again.")When I run this program, I run into the following problems:
- When I input an integer, there is no output, it just asks for the next input without printing the square.
- When I input a floating point number, it outputs "You did not input a number. Please try again.", which it should only do if the input wasn't a number (integer nor float).
I don't know what's going on with this, because what I had hoped to happen was the function would print out the square of the input (if the input was an integer or floating point number) or print ("You did not input a number. Please try again." (if the input was anything else).
Hopefully this problem shouldn't be too difficult for programming experts such as yourselves! I would be very grateful for your help!