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))
Related
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.
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
Struggling with this one. Only a beginner so go easy on me! :)
Trying to have user input password and if not within 6-10 characters. They are asked to input again.
When password correct the loop stops.
I can’t seem to get it to exit the loop even if correct lenght password entered.!
min_password_lenght = 6
max_password_lenght = 10
password = input(“enter password:”)
password_lenght = len(password)
while password_lenght > 6 or password_lenght < 10:
print(“error”)
password = input(“enter again:”)
print (“password correct”)
You should probably be more specific next time, but here's a snippet that satisfies what you described:
pwd = ""
while len(pwd) < 6 or len(pwd) > 10:
pwd = input("Enter password: ")
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 3 years ago.
Improve this question
Hey is there a way to print inputs like this:
Input:
Enter the minimum number of feet (not less than 0):
5
Enter the maximum number of feet (not more than 99):
10
How can one do this?
Simply add a new line to the input prompt with \n (Python newline character) like this:
minFeet = input("Enter the minimum number of feet (not less than 0): \n")
maxFeet = input("Enter the maximum number of feet (not more than 99): \n")
You can just do it like this:
print("Enter the minimum number of feet (not less than 0):")
minN = input()
print("Enter the maximum number of feet (not more than 99):")
maxN = input()
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))