How to make Python multiply user input with a given fractional number?
Question:
I’m making a currency exchange program, but NOK (the user input in Norwegian kroner) won’t multiply with 0,10 and 0,11.
NOK = input("Enter the amount you wish to convert: ")
print (f"Your choosen amount is {NOK} NOK")
print("What do you wish to convert it to?")
Question = input("EUR or USD")
if Question == "EUR":
print({NOK} * 0,10)
elif Question == "USD":
print({NOK} * 0,11)
else:
print("Please anwser 'EUR' or 'USD'")
Answers:
Good to see you’re taking up coding.
There are a few issues with the code you provided.
- You need to indent code inside if, elif, else blocks. This means insert a tab or 4 spaces before every line inside the block.
- Python uses the period for the decimal seperator, unlike the comma used in European countries.
- Do not use curly brackets when referencing variables.
- Your NOK input is taken as a string (text), whereas it needs to be an integer (number) to multiply it. This is done with
int()
around your input()
Additionally, you should not name your variables starting with capital letters as these are traditionally reserved for classes.
Try this instead:
nok = int(input("Enter the amount you wish to convert: "))
print(f"Your choosen amount is {nok} NOK")
print("What do you wish to convert it to?")
question = input("EUR or USD")
if question == "EUR":
print(nok * 0.10)
elif question == "USD":
print(nok * 0.11)
else:
print("Please anwser 'EUR' or 'USD'")
You don’t need to write NOK between {} and you have to transform the answer in integer to multiple it.
NOK = int(input("Enter the amount you wish to convert: "))
print ("You choosen amount is {NOK} NOK")
print("What do you wish to convert it to?")
Question = input("EUR or USD")
if Question == "EUR":
print(NOK * 0,10)
elif Question == "USD":
print(NOK * 0,11)
else:
print("Please anwser 'EUR' or 'USD'")
I’m making a currency exchange program, but NOK (the user input in Norwegian kroner) won’t multiply with 0,10 and 0,11.
NOK = input("Enter the amount you wish to convert: ")
print (f"Your choosen amount is {NOK} NOK")
print("What do you wish to convert it to?")
Question = input("EUR or USD")
if Question == "EUR":
print({NOK} * 0,10)
elif Question == "USD":
print({NOK} * 0,11)
else:
print("Please anwser 'EUR' or 'USD'")
Good to see you’re taking up coding.
There are a few issues with the code you provided.
- You need to indent code inside if, elif, else blocks. This means insert a tab or 4 spaces before every line inside the block.
- Python uses the period for the decimal seperator, unlike the comma used in European countries.
- Do not use curly brackets when referencing variables.
- Your NOK input is taken as a string (text), whereas it needs to be an integer (number) to multiply it. This is done with
int()
around yourinput()
Additionally, you should not name your variables starting with capital letters as these are traditionally reserved for classes.
Try this instead:
nok = int(input("Enter the amount you wish to convert: "))
print(f"Your choosen amount is {nok} NOK")
print("What do you wish to convert it to?")
question = input("EUR or USD")
if question == "EUR":
print(nok * 0.10)
elif question == "USD":
print(nok * 0.11)
else:
print("Please anwser 'EUR' or 'USD'")
You don’t need to write NOK between {} and you have to transform the answer in integer to multiple it.
NOK = int(input("Enter the amount you wish to convert: "))
print ("You choosen amount is {NOK} NOK")
print("What do you wish to convert it to?")
Question = input("EUR or USD")
if Question == "EUR":
print(NOK * 0,10)
elif Question == "USD":
print(NOK * 0,11)
else:
print("Please anwser 'EUR' or 'USD'")