Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stupid question
#1
Can someone tell me why my nothing happens when i execute my program. Here is my code and output:


total_cash=0
total_owed=0
total_candy=20
input_var=input("How many candies would you like? ")
if input_var == 1:
    total_cash = total_cash + 40
    total_candy = total_candy - 1
    print (total_cash)
    print (total_candy)
Microsoft Windows [Version 6.1.7601]
Copyright © 2009 Microsoft Corporation.  All rights reserved.

C:\Users\AdminB>cd\code

C:\Code>python sales.py
How many candies would you like? 1

C:\Code>

Moderator Larz60+: Added Python tags. Please do this in the future (see help, BBCODE)
Reply
#2
Hello!
The input is a string. So to compare against an integer you have to turn input_var to int.
total_cash=0
total_owed=0
total_candy=20

# the whole while is in case if the user inputs a letter or a symbol instead of an integer
while True:
    try:
        input_var=int(input("How many candies would you like? "))
    except ValueError:
        print('Incorect input!')

if input_var == 1: # or instead you can do if input_var == '1':
    total_cash = total_cash + 40
    total_candy = total_candy - 1
    print (total_cash)
    print (total_candy)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
The code snippet, provided by wavic will result in infinite loop, because he missed one break statement. Otherwise his explanation is correct and valid.

This

while True:
   try:
       input_var=int(input("How many candies would you like? "))
   except ValueError:
       print('Incorect input!')
should be

while True:
   try:
       input_var=int(input("How many candies would you like? "))
       break
   except ValueError:
       print('Incorect input!')
However, there is no need for this
if input_var == 1:
you don't compare the user input to every possible value. Instead use the user input as variable in the calculations. Of course, you need to take care if available candles are enough to meet user buy request.
Reply
#4
Quote:The code snippet, provided by wavic will result in infinite loop, because he missed one break statement. 
Well, I was in a hurry but this can't be an excuse. Thank you!  Smile
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stupid question int 2 514 Mar-21-2025, 07:24 AM
Last Post: int
  Reverse list items in a stupid way pyth0n123 4 3,555 Jul-28-2019, 02:52 PM
Last Post: pyth0n123
  stupid > comparison question adetheheat 2 3,667 Jan-11-2018, 01:04 PM
Last Post: adetheheat
  how do you use stupid ffmpeg ineedastupidusername 0 5,151 Nov-10-2017, 05:38 AM
Last Post: ineedastupidusername

Forum Jump:

User Panel Messages

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