Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:23:50+00:00 2026-06-17T19:23:50+00:00

I’m working on a program that simulates a vending machine. For some this section

  • 0

I’m working on a program that simulates a vending machine. For some this section of my code creates an infinite loop, I cant quite figure out why. I ran it through python tutor to see where my problem was, it seems that when my variable price_remaining = 5 it will sometimes not go through the elif statement. I’m not entirely sure what would trigger it one way or another. I’m going to copy and paste what I entered in to python tutor in hopes that someone can explain based on that.
EDIT: I just checked through python tutor some more, it appears to only occur when I have a
value for price_remaining ending with a 5. For example 2.05,0.05, 1.15, etc.

price_remaining = 2.55
price_remaining = price_remaining * 100
q_stock = 25
q_returned = -0
d_stock = 25
d_returned = 0
n_stock = 25
n_returned = 0
while price_remaining > 0:
    if price_remaining >=25 and q_stock > 0:
        price_remaining = price_remaining - 25
        q_stock = q_stock - 1
        q_returned = q_returned + 1
    elif price_remaining >=10 and d_stock > 0:
        price_remaining = price_remaining - 10
        d_stock = d_stock - 1
        d_returned = d_returned + 1
    elif price_remaining >=5 and n_stock > 0:
        price_remaining = price_remaining -5
        n_stock = n_stock - 1
        n_returned = n_returned + 1

print( q_returned)
print( d_returned)
print( n_returned

)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:23:51+00:00Added an answer on June 17, 2026 at 7:23 pm

    When I add the line

    print(repr(price_remaining), repr(n_stock))
    

    at the start of your while loop, the code produces

    254.99999999999997 25
    229.99999999999997 25
    204.99999999999997 25
    179.99999999999997 25
    154.99999999999997 25
    129.99999999999997 25
    104.99999999999997 25
    79.99999999999997 25
    54.99999999999997 25
    29.99999999999997 25
    4.999999999999972 25
    4.999999999999972 25
    4.999999999999972 25
    [.. forever ..]
    

    Because of how floating point numbers work (see here for a reminder), you can’t always perfectly represent each possible number, hence all the 9s.

    In any case, once price_remaining < 5, even if it’s only less by a tiny amount, your code doesn’t have any if branch which triggers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.