I'm struggling to print the remaining amount inside the input
I tried to remove the money the users enter from the cost choice but it doesn't work
prod = ["Coffee", "Coffee with milk", "Chocolate", "Chocolate with milk"]
cost = [1.5, 1.8, 2.1, 2.4]
m = [0.1, 0.2, 0.5, 1, 2, 5, 10]
item_number = len(prod)
print("\nYour choices:")
for number in range(0, item_number, 1):
print(number + 1,
prod [number],
'{0:.2f}€'.format(cost[number]))
print ("0 Exit")
choice = int(input("Please enter a choice between (1-4) or hit 0 for exit: ")) -1
if choice < item_number and choice >= 0:
print("Please enter", "{0:.2f}€".format(cost[choice]), 'in total')
else:
print("Exiting the program")
exit(0)
money = float(input("Please fill the remaining amount ; "))
while money < cost[choice]:
money += float(input("You still have to enter ", cost[choice] - money))
change = money - cost[choice]
print("Your change {0:.2f}€".format(change))
Does this Help?
prod = ["Coffee", "Coffee with milk", "Chocolate", "Chocolate with milk"]
cost = [1.5, 1.8, 2.1, 2.4]
m = [0.1, 0.2, 0.5, 1, 2, 5, 10]
item_number = len(prod)
print("\nYour choices:")
for number in range(0, item_number, 1):
print(number + 1,
prod [number],
'{0:.2f}€'.format(cost[number]))
print ("0 Exit")
choice = int(input("Please enter a choice between (1-4) or hit 0 for exit: "))
-1
if choice < item_number and choice >= 0:
print("Please enter", "{0:.2f}€".format(cost[choice]), 'in total')
else:
print("Exiting the program")
exit(0)
money = float(input("Please fill the remaining amount ; "))
while money < cost[choice]:
money += float(input("You still have to enter "+str("{:.2f}".format(cost[choice] - money))))
change = money - cost[choice]
Your problem is in this line.
money += float(input("You still have to enter ", cost[choice] - money))
which should be
money += float(input("You still have to enter " + str(cost[choice] - money)))
Make the calculation first (inside parentheses) then, convert it to a string to concentrate with the input string.
Related
I was following along with a youtube video to a ATM using python and I got expected expression. I dont know how to fix it. line 32, 37 and 40.
enter image description here
You can't have an elif after an else. This is because else will capture all the remaining cases. Indentation, meaning spaces or tabs before the line is very important in Python.
Here is what I think you meant to write:
balance = 1000
print("""
Welcome to Dutch Bank
Choose Transaction
1) BALANCE
2) WITHDRAW
3) DEPOSIT
4) EXIT
""")
while True:
option = int(input("Enter Transaction"))
if option == 1:
print ("Your Balance is", balance)
anothertrans = input("Do You Want to make another Transaction? Yes/No: ")
if anothertrans == "YES":
continue
else:
break
elif option == 2:
withdraw = float(input("Enter Amount To Withdraw"))
if (balance > withdraw):
total = balance - withdraw
print("Success")
print("Your New Balance is: ",total)
else:
print("Insufficient Balance")
elif option == 3:
deposit = float(input("Enter Amount To Deposit"))
totalbalance = balance + deposit
print("Sucess")
print("Total Balance Now is: ", totalbalance)
elif option == 4:
print("Thank You ForBanking With Dutch Bank")
exit()
else:
print ("No selected Transaction")
Is there a way to multiply the items from these variables
candy_name = ["Assorted Small Lollipops", "Assorted Flavours Small", "Assorted Flavours Large", "Large Lollipop", "100g Assorted Flavours Small", "100g Assorted Flavours Large", "Candy Cane", "100g Candy Canes"]
candy_price = [0.1, 0.05, 0.2, 0.5, 4.5, 6, 0.2, 5.5]
total = 0
candy_order = []
price_order = []
number_order = []
if candy in candy_name:
candy_order.append(candy)
print()
print(candy_order,"was added to you order")
print()
position = candy_order.index(candy)
number = input("How many %s would you like? "%(candy))
try:
int(number)
except ValueError:
print("Please use integers only.")
continue
if int(number) <= 0:
print("That was not an option, please try again. ")
continue
else:
number_order.append(number)
candy_total = int(number) * price
total = total + candy_total
keep_ordering = input("Would you like to add more to your order? ").lower()
if keep_ordering == "no":
break
elif keep_ordering == "yes":
continue
else:
print("That was not an option, please use yes or no.")
break
break
elif candy == " ":
break
I want to multiply all of the items in "number_order" by price_order.
I have been recommended something like this:
for c,p,n in (candy_order, candy_price, number_order):
print(c, p, n, p * n)
Your code could probably be optimised slightly like this to see if this solves your problem
candy_name = ["Assorted Small Lollipops", "Assorted Flavours Small", "Assorted Flavours Large", "Large Lollipop", "100g Assorted Flavours Small", "100g Assorted Flavours Large", "Candy Cane", "100g Candy Canes"]
candy_price = [0.1, 0.05, 0.2, 0.5, 4.5, 6, 0.2, 5.5]
candy_info = dict(zip(candy_name, candy_price))
candy_order, number_order, total = [], [], 0
while True:
candy = input("which candy do you want?")
while candy not in candy_info:
candy = input("input error, try again:")
candy_order.append(candy)
number = input("How many %s would you like? " % candy)
while not number.isdigit() or int(number) <= 0:
number = input("Only integers greater than 0 are allowed, try again: ")
number_order.append(int(number))
keep_ordering = input("Would you like to add more to your order? y/n")
while keep_ordering not in ["y", "n"]:
keep_ordering = input("Simply enter y or n, try again:")
if keep_ordering != "y":
for _candy, _number in zip(candy_order, number_order):
total += candy_info[_candy] * _number
print("Your total spend is %f" % total)
break
The program goes as follows, asks to choose a product, then asks for the amount of the choice, then the user inputs the amount, but if the amount is different from the declared values (mo), the program prints "wrong coins", but when the user inputs the correct coin and amount, it should print only the "change" part of the code. In my program, it prints "change" and then wrong coin value
prod = ["Coffe", "Coffee with milk", "Chocolate", "Chocolate with milk"]
cost = [1.5, 1.8, 2.1, 2.4]
mo = [0.1, 0.2, 0.5, 1, 2, 5, 10]
item_number = len(prod)
print("\nPick an item:")
for number in range(0, item_number, 1):
print(number + 1,
prod [number],
'{0:.2f}€'.format(cost[number]))
print ("0 Exit")
choice = int(input("Please pick an item from (1-4) or hit 0 to exit: ")) -1
if choice < item_number and choice >= 0:
print("You should input", "{0:.2f}€".format(cost[choice]), 'in total')
else:
print("Exiting the program")
exit(0)
money = float(input("How much do you enter?; "))
while money < cost[choice]:
money += float(input("You should insert "+str("{:.2f}".format(cost[choice] - money))))
if money != mo:
print("Invalid amount.\nPlease enter a valid coin: 0.1 / 0.2 / 0.5 / 1 / 2 / 5 / 10")
else:
print("Try again")
change = money - cost[choice]
print("Change {0:.2f}€".format(change))
The logic may be different
continuously ask for money (a while True allow to write the input once)
verify if the choosen is in the list not equal (a float can't be equal to a list)
increment your total money
stop if you have enough
money = 0
while True:
new_money = float(input(f"You should insert {cost[choice] - money:.2f}: "))
if new_money not in mo:
print("Invalid amount.\nPlease enter a valid coin: " + "/".join(map(str, mo)))
continue
money += new_money
if money >= cost[choice]:
break
change = money - cost[choice]
print(f"Change {change:.2f}€")
The other code, with some improvments
print("\nPick an item:")
for number in range(item_number, ):
print(f'{number + 1} {prod[number]} {cost[number]:.2f}€')
print("0 Exit")
choice = int(input("Please pick an item from (1-4) or hit 0 to exit: ")) - 1
if 0 < choice <= item_number:
print(f"You should input {cost[choice]:.2f}€", 'in total')
else:
print("Exiting the program")
exit(0)
I am in a beginners computer science class, and for my final I have to create a menu that includes all of my previous coding assignments using only the basic techniques that we have been taught. I have created a code that works, but the problem is my code also produces the word "none" in the output.
def options():
print ("Pick from the list")
print ("1. Grade Conversion")
print ("2. Temperature Conversion")
print ("3. Currency Conversion")
print ("4. Sum and Average")
print ("5. Heads or Tails Guessing Game")
print ("6. Fibonacci Sequence")
print ("7. Factorials")
print ("8. Multiplication Table")
print ("9. Guess the Number Game")
print ("10. Calculator")
print ("11. Exit")
def Student_A_Gets_A():
print ("Student A gets Grade A")
print (Score)
def Student_B_Gets_B():
print ("Student B gets Grade B")
print (Score)
def Student_C_Gets_C():
print ("Student C gets Grade C")
print (Score)
def Student_D_Gets_D():
print ("Student D gets Grade D")
print (Score)
def Student_F_Gets_F():
print ("Student F gets Grade F")
print (Score)
def Celsius():
Temperature = int(input ("Please enter Temp. in Celsius: "))
print ("Fahrenheit = ", (Temperature*(9/5))+32)
def Fahrenheit():
Temperature = int(input ("Please enter Temp. in Fahrenheit: "))
print ("Celsius = ", (Temperature-32)*(5/9))
def Currency_Conversion():
print ("Pick from the list")
print ("1. Dollar to Euro")
print ("2. Dollar to Peso")
print ("3. Euro to Dollar")
print ("4. Peso to Dollar")
print ("5. Euro to Peso")
print ("6. Peso to Euro")
def Dollar_to_Euro():
print ("You have selected 1 to convert Dollar to Euro")
Amount = int(input("Please enter the amount in dollar(s): "))
print ("$", Amount, " = ", "€",(Amount)*(0.813654))
def Dollar_to_Peso():
print ("You have selected 2 to convert Dollar to Peso")
Amount = int(input("Please enter the amount in dollar(s): "))
print ("$", Amount, " = ", "Mex$",(Amount)*(18.695653))
def Euro_to_Dollar():
print ("You have selected 3 to convert Euro to Dollar")
Amount = int(input("Please enter the amount in euro(s): "))
print ("€", Amount, " = ", "$",(Amount)/(0.813654))
def Peso_to_Dollar():
print ("You have selected 4 to convert Peso to Dollar")
Amount = int(input("Please enter the amount in peso(s): "))
print ("Mex$", Amount, " = ", "$",(Amount)/(18.695653))
def Euro_to_Peso():
print ("You have selected 5 to convert Euro to Peso")
Amount = int(input("Please enter the amount in euro(s): "))
print ("€", Amount, " = ", "Mex$",(Amount)*(22.98))
def Peso_to_Euro():
print ("You have selected 6 to convert Peso to Euro")
Amount = int(input("Please enter the amount in peso(s): "))
print ("$", Amount, " = ", "€",(Amount)/(22.98))
def options2 ():
print ("Select Operation")
print ("1. Add 2 numbers")
print ("2. Subtract 2 numbers")
print ("3. Multiply 2 numbers")
print ("4. Divide 2 numbers")
print ("5. Guessing Game")
print (options())
answer = int(input("Enter a choice from 1 through 10: "))
while (1):
if answer < 1 and answer > 11:
print ("Sorry, that is not an option. Enter a number from 1 through 11")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 1:
Score = int(input("Please enter Score: "))
if Score >= 90 and Score <= 100:
print (Student_A_Gets_A())
print (" ")
print (options ())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 80 and Score <= 89:
print (Student_B_Gets_B())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 70 and Score <= 79:
print (Student_C_Gets_C())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 60 and Score <= 69:
print (Student_D_Gets_D())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Score >= 50 and Score <= 59:
print (Student_F_Gets_F())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error: Score must be between 100 and 50 for grades A through F")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 2:
Celsius_or_Fahrenheit = int(input ("Please enter 1 for Celsius or 2 for Fahrenheit: "))
if Celsius_or_Fahrenheit == 1:
print (Celsius())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Celsius_or_Fahrenheit == 2:
print (Fahrenheit())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, must choose 1 for Celsius or 2 for Fahrenheit")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 3:
print (Currency_Conversion())
Currency_Conversion_answer = int(input("Enter a choice from 1 through 6: "))
if Currency_Conversion_answer == 1:
print (Dollar_to_Euro())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 2:
print (Dollar_to_Peso())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 3:
print (Euro_to_Dollar())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 4:
print (Peso_to_Dollar())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 5:
print (Euro_to_Peso())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif Currency_Conversion_answer == 6:
print (Peso_to_Euro())
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, number not within bounds")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 4:
number = int(input("How many numbers would you like to add: "))
counter = 0
average = 0
for j in range(1,number+1):
if j %10 == 0:
print (j,",")
elif j == number:
print (j)
else:
print (j,"," ,end="")
for i in range(1,number + 1):
counter += i
average = counter/number
print ("sum = ", counter)
print ("average = ", average)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 5:
import random
rand = random.randint(1,2)
guess = int(input("Guess Heads(1) OR Tails(2): "))
if guess is 1 or guess is 2 and guess is rand:
print ("You win")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif guess is 1 or guess is 2 and guess is not rand:
print ("You lose")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, number must be 1 or 2")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 6:
fib = int(input("How many Fibonacci numbers shall I generate? "))
x = 1
y = 1
print ("Fibonacci sequence up to", fib)
for i in range (1):
for i in range (1,fib+1):
x = y - x
y = x + y
if i %10 == 0 and i is not fib:
print (x, ", ")
elif i == fib:
print (x)
else:
print (x, ", ", end="")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 7:
Number = int(input("Factorial of what number do you want? Number must be less than or equal to 20. "))
if Number > 20:
print ("Error")
else:
Factorial = 1
for i in range(1,Number + 1):
print (i,)
Factorial = Factorial * i
print("Factorial of", Number, " is ",Factorial)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 8:
num = int(input("Enter multiplication table you want to see: "))
maximum = int(input("Enter max you want your table to go to: "))
counter = 0
print ("The table of", num)
for i in range (0,maximum):
counter = i + 1
print (counter, "*", num, "=", counter*num)
print ("Done counting...")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 9:
import random
number = int(input("Guess any number 1 to 10? "))
guess = random.randint(1,10)
acc = 0
while guess > 0:
if guess < number:
print ("Too High")
number = int(input("Guess any number 1 to 10? "))
acc+=1
elif guess > number:
print ("Too Low")
number = int(input("Guess any number 1 to 10? "))
acc+=1
elif guess == number:
acc+=1
break
print ("And it only took you", acc, "tries")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 10:
import random
rand = random.randint(1,4)
print (options2())
answer = int(input("Enter a choice from 1 through 5: "))
if answer > 5 and answer < 1:
print ("Sorry, that is not an option")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 1:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
print (x,"+",y,"=",x+y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 2:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
print (x,"-",y,"=",x-y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 3:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
print (x,"*",y,"=",x*y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 4:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
if y == 0:
print ("Error, denominator cannot be zero")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print (x,"/",y,"=",x/y)
elif answer == 5:
x=float(input("Please enter the first number: "))
y=float(input("Please enter the second number: "))
if rand == 1:
print (x,"+",y,"=",x+y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif rand == 2:
print (x,"-",y,"=",x-y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif rand == 3:
print (x,"*",y,"=",x*y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif rand == 4:
print (x,"/",y,"=",x/y)
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
else:
print ("Error, must choose from the aforementioned selections")
print (" ")
print (options())
answer = int(input("Enter a choice from 1 through 11: "))
elif answer == 11:
break
print ("You have exited")
Printing return values from functions, as #Patrick Haugh indicates. For example, line 82:
print (options())
runs the function options(), which prints out the alternatives, and then prints the return value from options(), which is None as you don't specify any return value. Instead code line 82 as:
options()
You need to correct this on a lot of places, and then for other functions as well, e.g. Celsius() etc.
How can i print out float if the result have decimal or print out integer if the result have no decimal?
c = input("Enter the total cost of purchase: ")
bank = raw_input("Enter the bank of your credit card (DBS, OCBC, etc.): ")
dbs1 = ((c/float(100))*10)
dbs2 = c-dbs1
ocbc1 = ((c/float(100))*15)
ocbc2 = c-ocbc1
if (c > 200):
if (bank == 'DBS'):
print('Please pay $'+str(dbs2))
elif (bank == 'OCBC'):
print('Please pay $'+str(ocbc2))
else:
print('Please pay $'+str(c))
else:
print('Please pay $'+str(c))
exit = raw_input("Enter to exit")
Example-Result
Enter the total cost of purchase: 250
Enter the bank of your credit card (DBS, OCBC, etc.): OCBC
Please pay $212.5
Enter the total cost of purchase: 250
Enter the bank of your credit card (DBS, OCBC, etc.): DBS
Please pay $225.0
You can try this, which simply uses Python's string formatting method:
if int(c) == float(c):
decimals = 0
else:
decimals = 2 # Assumes 2 decimal places for money
print('Please pay: ${0:.{1}f}'.format(c, decimals))
This will give you the following output if c == 1.00:
Please pay: $1
Or this output if c == 20.56:
Please pay: $20.56
Python floats have a built-in method to determine whether they're an integer:
x = 212.50
y = 212.0
f = lambda x: int(x) if x.is_integer() else x
print(x, f(x), y, f(y), sep='\t')
>> 212.5 212.5 212.0 212
Since there is a much simpler way now and this post is the first result, people should now about it:
print(f"{3.0:g}") # 3
print(f"{3.14:g}") # 3.14
def nice_print(i):
print '%.2f' % i if i - int(i) != 0 else '%d' % i
nice_print(44)
44
nice_print(44.345)
44.34
in Your code:
def nice_number(i):
return '%.2f' % i if i - int(i) != 0 else '%d' % i
c = input("Enter the total cost of purchase: ")
bank = raw_input("Enter the bank of your credit card (DBS, OCBC, etc.): ")
dbs1 = ((c/float(100))*10)
dbs2 = c-dbs1
ocbc1 = ((c/float(100))*15)
ocbc2 = c-ocbc1
if (c > 200):
if (bank == 'DBS'):
print('Please pay $'+nice_number(dbs2))
elif (bank == 'OCBC'):
print('Please pay $'+nice_number(ocbc2))
else:
print('Please pay $'+nice_number(c))
else:
print('Please pay $'+nice_number(c))