Python Forum
Supposedly simple program not working! Please help!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Supposedly simple program not working! Please help!
#1
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):
  • 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!
Reply


Messages In This Thread
Supposedly simple program not working! Please help! - by BenjaminDJ - Feb-20-2018, 06:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,208 Dec-14-2021, 12:23 AM
Last Post: supuflounder
  Enigma Program not working properly npd29 3 2,086 May-01-2020, 10:37 AM
Last Post: pyzyx3qwerty
  Simple IF statement not working as intended gortexxx 2 2,802 May-17-2018, 07:54 PM
Last Post: gortexxx
  Simple Eight-Puzzle not working! BenjaminDJ 2 3,204 May-04-2018, 12:17 PM
Last Post: BenjaminDJ
  Simple Python program not working AudioKev 3 3,231 Jan-23-2018, 09:57 PM
Last Post: Larz60+
  GTIN code validation program not working! AnonDxZ 1 2,825 Nov-29-2017, 11:25 PM
Last Post: Prrz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020