Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Considering the example below, why would anyone choose the first print instead of the second?
def main():
name = input("Input your name:")
age = int(input("Input you age:"))
print("Your name is " + name + " and you are " + str(age) + " years old.")
print("Your name is", name, "and you are" , age, "years old.")
if __name__ == "__main__":
main()
It seems to me that print("text", var, "text") is more straightforward, since there's no need to explicitly convert our int to str.
Are there any relevant use cases for the print("text"+str(var)+"text") format?
The usual way of doing this is formatting like:
print(f"Your name is {name} and you are {age} years old.")
Considering the example below, why would anyone choose the first print instead of the second?
Well, the first way (concatenation of strings and expressions converted to strings) is obsolete and unusable for me, since we have proper formatting (like my example above). We can construct any string we want straightforwardly and clearly.
If we apply the same logic to *args approach, as for me, the outcome is the same.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this 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'")
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'")
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I created an input() and I want user to enter specific values like kg or KG or Kg and make any other values return "invalid entry"
when I try to assign other values to the variable unit1, it is taking the last entry or it throws an error
That's my code:
weight=int(input("Enter weight: "))
unit= input("Kg or Lbs: ")
unit1= "kg"
unit2="Lbs"
if unit==unit1:
print(weight/0.45)
elif unit==unit2:
print(weight*0.45)
else:
print("invalid entry")
You should change the casing of the strings to the same. String comparisons are case sensitive, so the string Lbs is not same as lbs
str.lower() is your friend here :)
You should also check to make sure someone is entering an integer for your weight so your code doesn't crash. Try this!
while True:
try:
weight=int(input("Enter weight: "))
break
except:
print("Invalid value entered for weight")
unit= input("Kg or Lbs: ")
unit1= "kg"
unit2="lbs"
if unit.lower()==unit1:
print(weight/0.45)
elif unit.lower()==unit2:
print(weight*0.45)
else:
print("invalid entry")
You could also use string.upper() in case this suits you more
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to make a basic program that gets the name from a user and makes a "lucky number" by just adding a number based on if it greater or less than another number. What am i doing wrong here?
myName = input('Insert name: ')
myLuckynumber = int(input('Insert a number 1-9: '))
if myLuckynumber > 5:
Newlucky = int(myLuckynumber + 3)
print('Hello '+myName+'your lucky number is: '+Newlucky)
elif myLuckynumber < 5:
newLucky = int(myLuckynumber + 4)
print('Hello '+myName+'your lucky number is: '+newLucky)
else:
print('try again!')
You cannot add a str to an int, they need to be strings to concatenate
print('Hello '+myName+'your lucky number is: '+ str(Newlucky))
Or using something like str.format
print('Hello {} your lucky number is: {}'.format(myName, Newlucky))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am new in python, i want to do is limit the float input by the user only upto two decimal point, eg: 1.11, user is not allowed to input 1.111 or more than to after two decimal point . Thank you
You cannot restrict what the user inputs, but you can convert a float to have 2 decimal points:
value = float(input("Input your number: "))
print ("You inputted " + str(value))
new_value = "{:.2f}".format(value)
print ("After formatting, your number has become: " + str(new_value))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to make a program where I can ask someone's age - then tell them that on their next birthday they will be x many years old.
for example:
python = "How old are you?"
answer = "17"
python = "On your next birthday you will be 18 years old".
that is the program which I am trying to make however I am stuck on how to add 1 to the age of the person
How about this?
age += 1
or
age = age + 1
Make sure you are casting the user's input from a string to an integer.
In order to increase age by one, you need it to be an integer.
Try:
age = int(input("How old are you? ")
age += 1
print("On your next birthday you will be" + str(age) + "years old")
age = input('How old are you?')
print('On your next birthday you will be' + str(int(age)+1) + 'years old.')