How to add on python [closed] - python

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.')

Related

Printing int and float in Python [closed]

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.

Can i print two statements with different conditions in a single if statement in python? [closed]

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
In this code if a person if greater than 80 he/she should be displayed with different statement whereas the one with age greater than 18 different but if possible without adding anymore if statements
if 80 > given > 18:
print("you are eligible for voting")
else:
print("you are not")
Possible to do without ifs at all!
for age in (10, 19, 50, 88):
print('age', age, 'is', ['<18', '18:80', '>80'][
int((age - 18) / (80 - 18) + 1)])
Output:
age 10 is <18
age 19 is 18:80
age 50 is 18:80
age 88 is >80

First Python Program [closed]

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))

How to print inputs in new line [closed]

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()

How can I put the length of name needs to be bigger than 3 in python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to make sure that the user inputs a name which is more than 3 letters, or else the program will have to repeat the question until the user inputs something which is acceptable.
while True:
if len(name) < 3
name = input("What is the student\'s name?")
For example:
name = ""
while len(name) < 3:
name = input("What's the student's name?")
#if python2:
#name = raw_input("What's the student's name?")
EDIT:
Worth to remember that in Python2 input tries to "guess" input type and if someone will provide for example "23" input will parse it to int and len(name) will throw exception. To avoid it it might be usefull to call raw_input instead of input.
In Python3 input returns string.
You mean something like this?
x = ""
y = input("Insert Y=")
while len(x) < y:
x = input("Insert X=")

Categories

Resources