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
#2
1. input() returns str. so your y is of type str
2. int(y) will [try to] convert y to int, float(y) will [try to] convert y to float. In both cases neither int(y)==y or float(y)==y, i.e. your if statement will always evaluate to false given that there is no some other problem i.e. that eventually will raise an exception).
3. The if is evaluated from left to right - that is python will evaluate int(y)==y and only if there is need it will try to evaluate float(y)==y. Because you have or, if int(y)==y is True, the whole if will be evaluated True, without evaluate float(y)==y.
4. what happens when you enter integer int(y)==y is False, then float(y)==y is also False, so the entire if is False.
5. When you enter float int(y) raise an exception, so you get the except part executed and it prints "You did not input a number. Please try again."
Reply
#3
The if statement:
if int(y) == y or float(y) == y:
will never be true.
it asks:
if (integer y is equal to text y) or (float y is equal to text y) do the following:
of course this will not work, they are not and never can be equal
I don't even know why that statement is there.
Reply
#4
Moved to Homework.

(Feb-20-2018, 06:50 PM)BenjaminDJ Wrote: I haven't been informed of any limitations so I felt this query needn't be in the Homework forums.

Just because you don't know the limitations for the assignment, doesn't mean it's not an assignment.
Reply
#5
As I see it, 3 ways come to mind to collect "y", depending on what type of number you expect (i.e.: integer, float, complex or any of the 3). You then need to test if the user entered a number or a non-number.

Once you resolve the issue of "string" vs. "numeric", and you say you have no limitations, be sure and read Python's built in math library.

A final (very stern) request, break yourself of the habit of using single letter variables. Python strongly encourages descriptive names, whether it be variables, functions, classes, etc.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
Thanks all for replying so quickly!

Quote:I don't even know why that statement is there.

I realize now that I don't need the if statement. I've changed my code to this and it seems to work now, so thanks for the help everybody!:
def Square_3(x):
    for i in range(x):
        try:
            y = float(input("Please input a number: "))
            print(y**2)
        except:
            print("You did not input a number. Please try again.")
Quote:A final (very stern) request, break yourself of the habit of using single letter variables.
Thanks for the tip! I don't usually use one letter variables for my own programs, but just because this program was so small I thought there was no point in wasting time by giving variables very long names.
Reply


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,140 Dec-14-2021, 12:23 AM
Last Post: supuflounder
  Enigma Program not working properly npd29 3 2,062 May-01-2020, 10:37 AM
Last Post: pyzyx3qwerty
  Simple IF statement not working as intended gortexxx 2 2,770 May-17-2018, 07:54 PM
Last Post: gortexxx
  Simple Eight-Puzzle not working! BenjaminDJ 2 3,159 May-04-2018, 12:17 PM
Last Post: BenjaminDJ
  Simple Python program not working AudioKev 3 3,195 Jan-23-2018, 09:57 PM
Last Post: Larz60+
  GTIN code validation program not working! AnonDxZ 1 2,793 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