Python Forum
Trying to get an if..elif..else statement to run.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get an if..elif..else statement to run.
#1
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:


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
Reply
#2
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
Reply
#3
See the following thread
https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#4
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)
Reply
#5
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?"
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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