Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Need suggested alterations (6 function calculator) EwH006 Programmer named Tim Posts: 15 Threads: 5 Joined: Oct 2016 Reputation: -2 Likes received: 0 #1 Nov-08-2016, 02:14 PM (This post was last modified: Nov-08-2016, 03:29 PM by metulburr. Edited 2 times in total.) Good Morning, I've written a program for a project in my intro to python class called: 6 Function Calculator.  The functions are setup correctly. The part that isn't setup correctly is the 'while expression' or 'if expression' in line 36. I've used both if and while statements with the rest stayin the same. I commented out the while portion to show what else I was doing. The only thing that has not been changed on line 36 is the expression itself: integer1 <= 0 and integer1 <= 13 or integer2 <= 0 and integer2 <= 13: I've taken notice that if I try to input 0 on any of the two integers that it will produce invalid input, but if I try to put a number above 13 it doesn't, it keeps running. Even when typing in a 0 as input it still keeps going. NEED HELP! ```#######################################################################################################################   # Operation Functions     def add(x, y):             return x + y     def subtract(x, y):             return x - y     def multiply(x, y):             return x * y     def divide(x, y):             return x / y     def exponent(x, y):             return x ** y     def remainder(x, y):             return x % y   #######################################################################################################################   # User Input / Input Variables   integer1 = int(input("Enter an int between 1 \$ 12: ")) integer2 = int(input("Enter an int between 1 \$ 12: "))   # while integer1 <= 0 and integer1 <= 13 or integer2 <= 0 and integer2 <= 13:     # print("Invalid Choice")     # break   if integer1 <= 0 and integer1 <= 13 or integer2 <= 0 and integer2 <= 13:         print("Invalid Choice") else:     print("Select operation.")     print("1. (-A-) Addition Operation")     print("2. (-S-) Subtraction Operation")     print("3. (-M-) Multiplication Operation")     print("4. (-D-) Divisional Operation")     print("5. (-E-) Exponential Operation")     print("6. (-R-) Remainder Operation")   choice = input("Enter choice(A/S/M/D/E/R):")   if choice == 'A' or choice == 'a':     print(integer1, "+", integer2, "=", add(integer1, integer2)) elif choice == 'S' or choice == 's':     print(integer1, "-", integer2, "=", subtract(integer1, integer2)) elif choice == 'M' or choice == 'm':     print(integer1, "*", integer2, "=", multiply(integer1, integer2)) elif choice == 'D' or choice == 'd':     print(integer1, "/", integer2, "=", divide(integer1, integer2)) elif choice == 'E' or choice == 'e':     print(integer1, "^", integer2, "=", exponent(integer1, integer2)) elif choice == 'R' or choice == 'r':     print(integer1, "%", integer2, "=", remainder(integer1, integer2)) else:     print("Invalid Choice")``` nilamo Last Thursdayist Posts: 3,186 Threads: 82 Joined: Sep 2016 Reputation: 129 Likes received: 712 #2 Nov-08-2016, 04:12 PM A while loop doesn't make sense, because you only ever want the block to run once, AND you're not changing any of the conditions, so it'd be an infinite loop (if not for the "break"). And your condition can be phrased using english to say "if either integer1 or interger2 is less than or equal to 13, it's invalid". Based on your text, that doesn't sound right. Maybe you should be using >=13 instead of <=13? Also, mixing and/or like that can lead to some confusion. Once you have complicated conditions like that, you should use parenthases to make absolutely certain what you expect to happen is actually happening. Because AND has higher precidence than OR, what you have now is the same as this: `if (integer1 <= 0 and integer1 <= 13) or (integer2 <= 0 and integer2 <= 13):`Also (pt 2), an int will never be negative. Checking to see if it's negative is... a waste of your typing time :p EwH006 Programmer named Tim Posts: 15 Threads: 5 Joined: Oct 2016 Reputation: -2 Likes received: 0 #3 Nov-08-2016, 05:15 PM (This post was last modified: Nov-08-2016, 05:17 PM by EwH006.) (Nov-08-2016, 04:12 PM)nilamo Wrote: A while loop doesn't make sense, because you only ever want the block to run once, AND you're not changing any of the conditions, so it'd be an infinite loop (if not for the "break"). And your condition can be phrased using english to say "if either integer1 or interger2 is less than or equal to 13, it's invalid".  Based on your text, that doesn't sound right.  Maybe you should be using >=13 instead of <=13? Also, mixing and/or like that can lead to some confusion.  Once you have complicated conditions like that, you should use parenthases to make absolutely certain what you expect to happen is actually happening.  Because AND has higher precidence than OR, what you have now is the same as this: `if (integer1 <= 0 and integer1 <= 13) or (integer2 <= 0 and integer2 <= 13):`Also (pt 2), an int will never be negative.  Checking to see if it's negative is... a waste of your typing time :p Thanks for the reply, I've been trying to rewrite that whole line but still am having no luck. I can only use #'s 1-12 on the input and must produce invalid input if not. Also on the math operations the same thing, if a invalid choice is chosen, produce invalid choice. Also what is the keyword for terminating a whole program, or is there one. Is this where loops come in? All my sample runs work except #2 Sample run 1: This program will perform basic arithmetic operations on two integers between 1 and 12. Enter an int between 1 \$ 12: 2 Enter an int between 1 \$ 12: 3 (A)ddition (S)ubtraction (M)ultiplication (D)ivision ®emainder (E)xponentiation e 2 ^ 3 = 8 Sample run 2: This program will perform basic arithmetic operations on two integers between 1 and 12. Enter an int between 1 \$ 12: 3 Enter an int between 1 \$ 12: 15 Invalid Choice Sample run 3: This program will perform basic arithmetic operations on two integers between 1 and 12. Enter an int between 1 \$ 12: 3 Enter an int between 1 \$ 12: 6 (A)ddition (S)ubtraction (M)ultiplication (D)ivision ®emainder (E)xponentiation g Invalid Choice Sample run 4: This program will perform basic arithmetic operations on two integers between 1 and 12. Enter an int between 1 \$ 12: 9 Enter an int between 1 \$ 12: 3 (A)ddition (S)ubtraction (M)ultiplication (D)ivision ®emainder (E)xponentiation d 9 / 3 = 3.0 Larz60+ aetate et sapientia Posts: 7,668 Threads: 289 Joined: Sep 2016 Reputation: 286 Likes received: 1059 #4 Nov-08-2016, 07:36 PM (This post was last modified: Nov-08-2016, 07:36 PM by Larz60+. Edited 1 time in total.) The compound if can just be ```while integer1 <= 13 or integer2 <= 13: ```because <= 0 is <= 13 EwH006 Programmer named Tim Posts: 15 Threads: 5 Joined: Oct 2016 Reputation: -2 Likes received: 0 #5 Nov-08-2016, 08:33 PM Thanks for those that gave their input. I was able to get the homework to grade at 100%. Larz60+: your while statement: 'while integer1 <= 13 or integer2 <= 13:' didn't work, I had to make the 'or' an 'and'. Also if I use the if statement of this, how would I break or terminate the program like I do when using the while statement? nilamo Last Thursdayist Posts: 3,186 Threads: 82 Joined: Sep 2016 Reputation: 129 Likes received: 712 #6 Nov-08-2016, 08:39 PM (Nov-08-2016, 08:33 PM)EwH006 Wrote: Also if I use the if statement of this, how would I break or terminate the program like I do when using the while statement? I'm not sure you know how if statements work. Larz60+ aetate et sapientia Posts: 7,668 Threads: 289 Joined: Sep 2016 Reputation: 286 Likes received: 1059 #7 Nov-08-2016, 11:13 PM (This post was last modified: Nov-08-2016, 11:13 PM by Larz60+. Edited 1 time in total.) EWH006- I was just showing that your statement was not what you wanted it to be. the one I wrote did exactly what yours did: this was your original: ```# while integer1 <= 0 and integer1 <= 13 or integer2 <= 0 and integer2 <= 13:    # print("Invalid Choice")    # break ```I think you meant to write >0 snippsat Posts: 3,767 Threads: 82 Joined: Sep 2016 Reputation: 255 Likes received: 1177 #8 Nov-09-2016, 06:30 PM (This post was last modified: Nov-09-2016, 09:08 PM by snippsat. Edited 3 times in total.) (Nov-08-2016, 11:13 PM)EwH006 Wrote: Also what is the keyword for terminating a whole program, or is there one. Is this where loops come in? Yes to run as shown under you need a running loop. It's hard if you have to much in global space,keep it in functions. So in this example there is function menu(),that always loop until Quit. You always fall back into this function for new choice. I just use one add() function to show the point. ```def add(user_input):    integer1, integer2 = user_input()    print('{} + {} = {}'.format(integer1, integer2, integer1+integer2)) def user_input():    integer1 = int(input("Enter an int between 1 \$ 12: "))    integer2 = int(input("Enter an int between 1 \$ 12: "))    return integer1, integer2 def show_options():    print("1. (-A-) Addition Operation")    print("2. (-S-) Subtraction Operation")    print('(Q) Quit\n') def menu():    show_options()    while True:        choice = input('Enter your choice: ').lower()        if choice == '1':            add(user_input)        elif choice == '2':            pass        elif choice == 'q':            return False        else:            print('Not a correct choice: {}'.format(choice)) if __name__ == '__main__':    menu()``` « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)