Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Trying to get an if..elif..else statement to run. Azurato Unladen Swallow Posts: 2 Threads: 1 Joined: Jul 2019 Reputation: 0 Likes received: 0 #1 Jul-28-2019, 03:07 PM I am having difficulties getting an if...elif..else statement to run in a function. ``` listOfWhatIsOwedBack = [i for i in str(whatIsOwedBack)] if listOfWhatIsOwedBack[-1] == str(1) or str(2): listOfWhatIsOwedBack[-1] == str(0) elif listOfWhatIsOwedBack[-1] == str(3) or str(4) or str(6) or str(7): listOfWhatIsOwedBack[-1] == str(5) else: listOfWhatIsOwedBack[-1] == str(8) or str(9) listOfWhatIsOwedBack[-1] == str(0) print(listOfWhatIsOwedBack) ```The rest of the code is in here to save space: Hide/Show ``` def afterPennyAbolishCoinsLeftAfterCheck(): print("Enter the floating point numbers representing the item cost and the amount paid respectively") itemPrice = input() moneyGiven = input() anythingLeft = moneyGiven - itemPrice if anythingLeft < 0: whatIsOwedBack = anythingLeft * -1 moduloHundredCanadian = whatIsOwedBack % 100 moduloFiftyCanadian = moduloHundredCanadian % 50 moduloTwentyCanadian = moduloFiftyCanadian % 20 moduloToonie = moduloTwentyCanadian % 2 moduloLoonie = moduloToonie % 1 moduloQuarter = moduloLoonie % 0.25 moduloDime = moduloQuarter % 0.10 dividedHundredCanadian = whatIsOwedBack // 100 dividedFiftyCanadian = moduloHundredCanadian // 50 dividedTwentyCanadian = moduloFiftyCanadian // 20 dividedToonie = moduloTwentyCanadian // 2 dividedLoonie = moduloToonie // 1 dividedQuarter = moduloLoonie // 0.25 dividedDime = moduloQuarter // 0.10 dividedNickel = moduloDime // 0.05 print("Here's your change: " + str(dividedHundredCanadian) + " 100\$ Bill(s), " + str(dividedFiftyCanadian) + " 50\$ Bill(s), " + str(dividedTwentyCanadian) + " 20\$ Bill(s), " + str(dividedToonie) + " Toonie(s), " + str(dividedLoonie) + " Loonies, " + str(dividedQuarter) + " Quarter(s), " + str(dividedDime) + " Dime(s) and " + str(dividedNickel) + " Nickel(s)") else: print("You still need to pay this much: " + str(anythingLeft)) listOfWhatIsOwedBack = [i for i in str(whatIsOwedBack)] if listOfWhatIsOwedBack[-1] == str(1) or str(2): listOfWhatIsOwedBack[-1] == str(0) elif listOfWhatIsOwedBack[-1] == str(3) or str(4) or str(6) or str(7): listOfWhatIsOwedBack[-1] == str(5) else: listOfWhatIsOwedBack[-1] == str(8) or str(9) listOfWhatIsOwedBack[-1] == str(0) print(listOfWhatIsOwedBack) ``` listOfWhatIsOwedBack would be a list of integers converted from an integer with 2 decimal places. Calling the function produces this: ``` afterPennyAbolishCoinsLeftAfterCheck() Enter the floating point numbers representing the item cost and the amount paid respectively 302.57 3.03 Here's your change: 2.0 100\$ Bill(s), 1.0 50\$ Bill(s), 2.0 20\$ Bill(s), 4.0 Toonie(s), 1.0 Loonies, 2.0 Quarter(s), 0.0 Dime(s) and 0.0 Nickel(s) ['2', '9', '9', '.', '5', '4'] ```With 302.57 and 3.03 being the numbers inputted into the input command. The goal of this task is to make it so that the last integer on that list is updated into either a 0 if it was a 1, 2, 8 or 9, or a 5 if it was a 4, 5, 6 or 7. I have tried leaving the specified numbers as just integers without converting them with str(), with no difference observed. I have attempted to refer to the position in the list as [-1::] instead of just [-1] to see if approaching it as a slice would make any difference, it did not. I have attempted to define the integers into variables, for instance one = 1, two = 2 and so on to see if that affected anything. It did not. Adding brackets after the if and elif to cover the statements also did not appear to change the results. I have also attempted to research examples of if...elif...else statements, but I can't figure out from the examples given such as on geeksforgeeks or stackoverflow anything in particular that would make my code just appear to skip over the if...elif...else statement. My understanding of Python so far has me think that after running through the if...elif...else statement, the variable listOfWhatIsOwedBack should have been updated within the function to whichever outcome the if...elif...else statement dictated, but that just does not seem to have happened. I am using Python version 3.7.4 on a MacOS Sierra Version 10.12.4. The code was written in Atom with the hydrogen, hydrogen-python and terminal packages installed from the Atom packages viewer. Thank you ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 271 Likes received: 1262 #2 Jul-28-2019, 03:24 PM (This post was last modified: Jul-28-2019, 03:24 PM by ichabod801. Edited 1 time in total. Edit Reason: reason is for rational people. ) You're using comparison operators (==) for your assignments. Use one equals (=) to assign a value. When working with money, it is often best to doing everything in cents using integer values. That way you avoid any screwiness with floating point values. Note that '1' is equivalent to str(1), easier to type, and probably more efficient. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures Yoriz Posts: 1,151 Threads: 28 Joined: Sep 2016 Reputation: 101 Likes received: 259 #3 Jul-28-2019, 03:29 PM See the following thread https://python-forum.io/Thread-Multiple-...or-keyword ichabod801 likes this post Azurato Unladen Swallow Posts: 2 Threads: 1 Joined: Jul 2019 Reputation: 0 Likes received: 0 #4 Jul-29-2019, 10:57 AM I have changed the relevant code to be as such after debugging it: ``` whatIsOwedBack = anythingLeft * -1 listOfWhatIsOwedBack = [i for i in str(whatIsOwedBack)] if (listOfWhatIsOwedBack[-1] == "1" or listOfWhatIsOwedBack[-1] == "2"): listOfWhatIsOwedBack[-1] = "0" elif (listOfWhatIsOwedBack[-1] == "3" or listOfWhatIsOwedBack[-1] == "4" or listOfWhatIsOwedBack[-1] == "6" or listOfWhatIsOwedBack[-1] == "7"): listOfWhatIsOwedBack[-1] = "5" else: listOfWhatIsOwedBack[-1] == "8" or listOfWhatIsOwedBack[-1] == "9" listOfWhatIsOwedBack[-1] = "0" print(listOfWhatIsOwedBack) ```After following the link and taking the suggestions into account, it appears that nothing has changed, except that it seems to have stopped reading the code after either print statement, not even reading the variable listOfWhatIsOwedBack. ```afterPennyAbolishCoinsLeftAfterCheck() Enter the floating point numbers representing the item cost and the amount paid respectively 302.57 3.03 Here's your change: 0.0 100\$ Bill(s), 0.0 50\$ Bill(s), 0.0 20\$ Bill(s), 1.0 Toonie(s), 0.0 Loonies, 3.0 Quarter(s), 2.0 Dime(s) and 0.0 Nickel(s) ``` ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 271 Likes received: 1262 #5 Jul-29-2019, 12:17 PM (This post was last modified: Jul-29-2019, 04:41 PM by Yoriz. Edited 1 time in total.) First, your initial conditional is backwards. anythingLeft is paid minus price. You check if that's negative, and if it is, you give change. But if paid minus price is negative, than the price was greater than what was paid, so you still owe money. That conditional should be >= 0, not < 0. Second, you have: ```elif (listOfWhatIsOwedBack[-1] == "3" or listOfWhatIsOwedBack[-1] == "4" or listOfWhatIsOwedBack[-1] == "6" or listOfWhatIsOwedBack[-1] == "7"): ```That works, but you can simplify it with: ```elif (listOfWhatIsOwedBack[-1] in ("3", "4", "6", "7"): ```Third, you have this bit: ``` else: listOfWhatIsOwedBack[-1] == "8" or listOfWhatIsOwedBack[-1] == "9" listOfWhatIsOwedBack[-1] = "0" ```The second line there is using comparison and or operators, so it looks like it should be a condition. And if you look that the logic of that last if/elif, it's there to adjust for there being no pennies. So the else case should be '5' or '0', and the first two lines in the above snippet should be combined to make another elif: ``` elif listOfWhatIsOwedBack[-1] == "8" or listOfWhatIsOwedBack[-1] == "9": listOfWhatIsOwedBack[-1] = "0" ```But in this case you are rounding up the change, correct? So you also need to increase listOfWhatIsOwedBack[-2] by one. Finally, you are using descriptive variable names. This is good. It is clear what your code is doing because of that. We see a lot of code with x, i, w, b, and it's hard to figure out the code enough to help. But you can over do it. Take 'afterPennyAbolishCoinsLeftAfterCheck'. That's 36 characters long. I've worked in programming languages where that's too long to even be a valid name. A study has actually been done on this, and it suggests that 7 to 12 characters is a good length. Longer than that increases the number of errors, as you have to remember and type the longer names. That's not a hard and fast rule, but if you are going over 12 characters, think to yourself, "Could I make this shorter and have it still be clear?" Yoriz wrote Jul-29-2019, 04:41 PM:Nominated for Weekly Top Pick - 29th July To 4th August 2019 https://python-forum.io/Thread-29th-July...6#pid87446 Yoriz likes this post Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)