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


Messages In This Thread
Trying to get an if..elif..else statement to run. - by Azurato - Jul-28-2019, 03:07 PM

Forum Jump:

User Panel Messages

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