This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 6 years ago.
This is what I am trying to code: ask the user to deposit an amount of money of at least $20 to open the shopper card.
The store will then give the first-time bonus of $10 to spend on items.
The program will total the amount and generate the balance.
If the amount originally deposited is less than $20, the customer will be told that the amount is too little to open the account (use of conditional expression).
If the amount is greater than or equal to $20, then $10 will be added, and the total amount on the shopper card will be displayed.
So far I have done this much but have gotten stuck.
print ('''Hi my name is Richard. Welcome to Buymore.''')
user = input( 'First name : ')
user = input( 'Last name : ')
user = input('ZIP code : ')
print ("Please deposit$20 into your account for your new shopper's card.")
user=input('20 dollars : ')
if num <20:
print ("Congratulation’s on opening your new card")
print ("As a gift we are giving you 10 dollars towards your first purchase")
if num >20 :
print('Need to deposit more minimum is 20 dollars')
Stuck at what level? What is working and was isn't?
From looking at your code, the condition should be reversed.
if num >= 20:
# this is ok, give bonus.
else :
#give error
The user input should be retrieved in a num variable, and converted to an integer.
int(num)
retrieving the user information like you do is useless since you overwrite the name with the zip code, get them in separate variables.
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 6 months ago.
My goal is to write code that allows people to place blind bids and then display the winner from the max value of the dictionary at the end. It works perfectly within inputs of the same digits (ex. 10 and 50, 100 and 500) but decides that 500 is the winner vs 1000, or 5000 is the winner against 11000. Im not sure why it decides the lower value is greater. I have tested a multitude of inputs and bids and it seems that each time you add another digit to the input it bugs.
def cls():
os.system('cls' if os.name=='nt' else 'clear')
bid_dict = {}
def winning():
while True:
winner = max(bid_dict, key=bid_dict.get)
price = max(bid_dict.values())
print(f'The winner is {winner} with a bid of ${price}')
print(bid_dict)
break
while True:
name = input("What is your name? ")
bid = input('What is your bid? ')
bid_dict[name] = bid
more = (input("Are there more bidders? ")).lower()
if more == "yes":
cls()
continue
else:
cls()
winning()
By default, input() returns a string. So when you compare the values using max(), it sorts them as strings and returns the string that starts with the highest value (i.e. the string that starts with the largest number).
You need to save the bids as some sort of number; either an int or a float would work, depending on whether you want to allow decimal places. That will allow you to compare them as actual numbers.
This question already has answers here:
Why does multiplication repeats the number several times? [closed]
(7 answers)
Closed 1 year ago.
I’ve written a piece of code that instead of print the product, prints a number a certain number of times. Whats wrong with it?
twelve = 12
name = input("What is your name? \nAnswer: ")
print("Cool name!")
nums = input("\n\nHow much pocket money did you receive last month?\nAnswer: ")
total = nums * twelve
print("\n\nI think you get ", total + " pounds in pocket money per year! Nice!")
The reason is that your nums variable is a string, which is the default with all Python inputs. Try converting it to int:
nums = int(input(...))
Or float if you are inputting a floating point number.
This question already has answers here:
Fastest way to check if a value exists in a list
(11 answers)
Closed 2 years ago.
Hi I am new to programming and I like to try to make the code work different way.
Unfortunately, variable called unlucky cant be read when printing.
So when the user enter the listed value [7 6 5], it doesnt print out as "You have selected unlucky number". I have include the image and copy of my code :
option = int(input("Guess the correct number between 0 to 10 : \nYour choice :"))
unlucky_number= [7,6,5] # Unlucky numbers, listed
if option == unlucky_number: # should print somewhere close when user enters list number
print("You have selected a unlucky number")
elif option== 6: # only 6 is correct
print ("Correct Guess")
elif option in range (0,4):
print("Not close")
else:
print ("Not in the range, choose between 1 to 10")
Please tell me whats wrong with it and how can I make a better version of it.
Thank you enter image description here
if option == unlucky_number
This line is causing you troubles. Your "option" holds a singular number, but your "unlucky_number" is a list. List can not be equal to a number, they are completely different animals. What you want is:
if option in unlucky_number
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am really new to Python and programming (2 days to be exact). I was messing around in idle trying to come up with a more complicated process than just print "Hello world." I was wondering if any of you could tell me why it is marking my print and elif statements invalid. I am using Python 2.7.10, thanks!
A = raw_input("Do you live in the US or Canada?")
if A == " US" or "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
B = int(raw_input("How much is your package?")
if B >= 25
print "Your shipping is $4.00"
elif B >= 50
print "Your shipping is $8.00"
else:
print "Congrats, your shipping is free!"
Probably the first thing you notice about python is that consistent indentation is not just a good idea, it is mandatory. Experienced programmers, whether they write Python or not, always do that anyway so it is no big deal. Use spaces (4 is the norm) and avoid tabs - change your editor to replace a tab with 4 spaces.
It is a bad idea to use float on money amounts because of rounding. Better to use a large type, like Decimal, or store the amount as an int in cents, then insert a decimal point when you display. For simplicity I have stuck with using float, but be warned.
You have a number of logic errors in your code, as well as issues with style. Programming style is not just about what looks nice, it is whether you can understand your code later, when you come back to it.
Style points:
Don't use UPPERCASE for variables. By convention UPPERCASE is reserved for constants
Use meaningful variable names, not A and B
Here is a corrected program, with comments. Please read the comments! :
# This is a comment, it is ignored by python
# This is used later on by sys.exit()
import sys
# Logically the user would enter "Yes" or "No" to this quesion,
# not US or Canada!
ans = raw_input("Do you live in the US or Canada? ") # Notice the space after ?
# Note how the condition has been expanded
if ans == "US" or ans == "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
# Now what? Your program just carried on. This will stop it
sys.exit()
# I'm using a floating point number for simplicity
amount = float(raw_input("How much is your package? "))
# I changed this around, since 50 is also >= 25!
# However, this is strange. Usually the more you spend the LESS the shipping!
# You were missing the : after the condition
if amount >= 50:
print "Your shipping is $8.00"
amount += 8 # This adds 8 to the amount
elif amount >= 25:
print "Your shipping is $4.00"
amount += 4 # This adds 4 to the amount
else:
print "Congrats, your shipping is free!"
# print the amount showing 2 decimal places, rounding
print "Amount to pay: $%.2f" % (amount)
You have plenty more to do. Maybe cope with the user entering lower or mixed case letters for the country name - and ask yourself if the question is logical to the user.
Later you might want to have a list of valid countries, and use in to test if the user entered a valid country. Then expand it to use a dictionary, indicating currency symbols, shipping amounts, and currency conversion rates for each country.
Enjoy Python!
Fixed it for you :). Take a look to see what's different, and you will learn, young padawan:
A = raw_input("Do you live in the US or Canada?")
if A == "US" or A == "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
B = float(raw_input("How much is your package?"))
if B >= 25:
print "Your shipping is $4.00"
elif B >= 50:
print "Your shipping is $8.00"
else:
print "Congrats, your shipping is free!"
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 months ago.
I need to create a code where the user can input a certain number of courses, and then it will take the gpa of them, but how can I change the variable name in the loop?
I have this so far
number_courses= float(input ("Insert the number of courses here:"))
while number_courses>0:
mark_1= input("Insert the letter grade for the first course here: ")
if mark_1=="A+" :
mark_1=4.0
number_courses= number_courses-1
If I want to change the variable name of mark_one to something different each time I go through the loop, what is the simplest way I can do this? And also is it possible to change it in my input statement to ask for first, second, third... as I go through the loop? I have tried searching on google, but none of the answers I can understand as their code is far behind my level or they didn't seem to answer what I needed either. Thanks.
You want to use a list or something similar to gather the input values:
number_courses=input("Insert the number of courses here: ")
marks = []
while number_courses>0:
mark = input("Insert the letter grade for the first course here: ")
if mark == "A+":
mark = 4.0
marks.append(mark)
number_courses -= 1
print marks
Use a dictionary:
number_courses= float(input ("Insert the number of courses here:"))
marks = {'A+':4, 'A':3.5, 'B+':3}
total_marks = 0
while number_courses:
mark_1= input("Insert the letter grade for the first course here: ")
if mark_1 in marks:
total_marks += marks[mark_1] #either sum them, or append them to a list
number_courses -= 1 #decrease this only if `mark_1` was found in `marks` dict