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.
#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


Messages In This Thread
RE: Trying to get an if..elif..else statement to run. - by ichabod801 - Jul-29-2019, 12:17 PM

Forum Jump:

User Panel Messages

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