I just started working with python 3 and I'm using a command shell. Why is there an exception with the code below?
name = input("whats your name: ")
age = input("what is your age: ")
work = input("how long will you be working: ")
print("Good luck " + name + " you will be " + int(age) + int(work) + " years old")
Python debugger generates error "should be str vs int".
The problem is string + integer doesn't work (for good reason). Instead we need to convert back to a string in your method.
But don't write strings like that. As you can see it is pretty error prone. Instead, use string formatting
print("Good luck {} you will be {} years old".format(name, int(age) + int(work)))
or even better in python 3.6
print(f"Good luck {name} you will be {int(age) + int(work)} years old")
Try this:
print("Good luck " + name + " you will be " + str(int(age)) + int(work)) + " years old")
Most likely because you are concatenating strings and adding ints at the same time. Add them together then convert to string and concatintate afterwards.
Ideally you are converting the str to int by int(age) and again trying to concatenate string with integer. By default, the input() gets the data in string.
please avoid using int() conversion. Also if needed check type(var) and try to concatenate.
Related
name = input("what is your name : ")
age = int(input("what is your age : "))
age_after_100_years = 2021 + (100-age)
print(name + " your age after 100 years is " + age_after_100_years)
in the above code on line 2, ive converted the string to int and then used it in "age_after_100_years" variables, but it gives me an error
Output:
what is your name : p
what is your age : 25
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-565602469d9c> in <module>
2 age = int(input("what is your age : "))
3 age_after_100_years = 2021 + (100-age)
----> 4 print(name + " your age after 100 years is " + age_after_100_years)
TypeError: can only concatenate str (not "int") to str
But when i use str() on line 3 i.e
name = input("What is your name? : ")
age = int(input("What is your age? : "))
age_after_100_years = str((2022-age)+100)
print(name+" you will be 100 years old by the year "+age_after_100_years)
Output:
What is your name? : pratik
What is your age? : 25
pratik you will be 100 years old by the year 2097
the above code works,
as i want to know, i've already converted the string to int() in variable "age", then why do i have to convert variable "age_after_100_years" to str(), if my "age" variable is int() and "age_after_100_years" has int() inputs to begin with, and i am concatenating int with int inputs?
The variable "age_after_100_years" is int, so you have to convert it to string if you want to concatenate it to another string using "+" operator.
The + operator in Python does addition for numerical values (for example, 1 + 2 = 3) but it does concatenation for strings ("1" + "2" = "12"). From what Python is telling you, you're attempting to + a string and a number, which is not allowed. Instead, you need to convert the number to a string value (str(number_variable)) before doing a concatenation.
Or, as others have noted, use f-strings, which allow you to substitute your numbers (or any other types/objects which support str()) into a string expression (actually, anything, but you might get text such as "BinaryTree object at 0x0454354" if you call str() on a BinaryTree class that has not implemented an interface for the str() method).
Looking at your example, you might want to do:
name = input("What is your name? : ")
age = int(input("What is your age? : "))
age_after_100_years = str((2022-age)+100)
# passing multiple parameters to print()
print(name, " you will be 100 years old by the year ", age_after_100_years)
# using string concatenation
print(name + " you will be 100 years old by the year " + str(age_after_100_years))
# using f-strings
print(f"{name} you will be 100 years old by the year {age_after_100_years}")
# using %-formatting
print("%s you will be 100 years old by the year %s" % (name, age_after_100_years) )
# using string.format()
print("{} you will be 100 years old by the year {}".format(name, age_after_100_years) )
All should get the job done, but f-strings are cleaner, shorter, and easier to read :)
friend your code id giving an error because the age variable in it is of int type and in the next line you are concatenating the int which will not be allowed
So I have corrected your code and after correction
this is the code
name = input("what is your name : ")
age = int(input("what is your age : "))
age_after_100_years = 2021 + (100-age)
print(name , " your age after 100 years is " , age_after_100_years)
and the result after that is
what is your name : p
what is your age: 12
p your age after 100 years is 2109
I am trying to execute this but not able to.
Can someone assist?
teamname1 = print(input((plyer1,' Name of your team? '))
teamname2 = print(input(plyer2,' Name of your team? '))
print(teamname1)
print(teamname2)
Three issues:
the first line contains one parenthesis too many
input() takes only one argument, the prompt. If plyer1 is a
string, you must concatenate it
same as in comment: print() does not return anything, and can be
omitted because the prompt of the input() command is already
displayed.
You probably need something like this:
plyer1 = 'parach'
plyer2 = 'amk'
teamname1 = input(plyer1 + ' Name of your team? ')
teamname2 = input(plyer2 + ' Name of your team? ')
print(teamname1)
print(teamname2)
I'm not exactly sure what you're trying to do with the teamname variables. If you could modify the question/code it would help a lot.
As far as print and input on the same line, I think this might be what you're going for.
print("player1" + " " + input("Name of your team: "))
print("player2" + " " + input("name of your team: "))
P.S. There are many tutorials on-line that could help. Make sure to look around first, then come here.
first of all, i'm very new at programming and starting my courses in college, i'm still trying to get the hang of it, please be nice. // i'm doing homework and it's this thing about calculating my age in the year of 2050, i've followed the instructions and everything and i get a syntax error in the print("I will be") line, can anyone tell me why and what am i doing wrong?
YEAR = "2050"
myCurrentAge = "18"
currentYear = "2019"
print("myCurrentAge is" + str(myCurrentAge)
myNewAge = myCurrentAge + (YEAR - currentYear)
print("I will be" + str(myNewAge) + "in YEAR.")
stop
First, as #ObsidianAge pointed out, your print("myCurrentAge is" + str(myCurrentAge) line is missing the second closing parenthesis, it should be like this print("myCurrentAge is" + str(myCurrentAge)).
Next is the error that you are talking about. You are calculating here myNewAge = myCurrentAge + (YEAR - currentYear) a bunch of string variables. You need to parse first your variables into int like this :
myNewAge = int(myCurrentAge) + (int(YEAR) - int(currentYear))
#then print,
print("I will be " + str(myNewAge) + " in YEAR" + YEAR)
To solve the confusion in the comments and I saw that you are following this answer's suggestion so here's the entire code:
# int variables
YEAR = 2050
myCurrentAge = 18
currentYear = 2019
# printing current age
print( "myCurrentAge is " + str(myCurrentAge))
# computing new age, no need to parse them to int since they already are
myNewAge = myCurrentAge + (YEAR - currentYear)
# then printing, parsing your then int vars to str to match the entire string line
print("I will be " + str(myNewAge) + " in YEAR " + str(YEAR))
Also, after you fix the bracket thing mentioned by #ObsidianAge, you will have to fix another thing.
All your variables are Strings right now since you have declared them within double-quotes. To be able to do what you intend to do, you will need to convert them to integers.
I'd recommend assigning them integer values in the very beginning by removing the quotes. With your current code, it will just concatenate all your variable strings together when you use the + operator. So your new declarations will look like this:
YEAR = 2050
myCurrentAge = 18
currentYear = 2019
This question already has answers here:
Print Combining Strings and Numbers
(6 answers)
Closed 5 years ago.
I would like to include the user's response to 'age' in the answer.
However, including 'age' returns an error. Seems to be because it is preceded by float. Is there a way to include the response to 'How old are you?'?
Here's the code:
name = input("What is your name? ")
age = float(input("How old are you? "))
answer1 = (age + "!? That's very old, " + name + "!")
answer2 = (age + "? You're still young, " + name + ".")
if age > 60:
print(answer1)
if age < 60:
print(answer2)
Unlike e.g. Java, Python does not automagically call the __str__ method when concatenating objects some of which are already strings. The way you do it, you would have to convert it to a string manually:
answer1 = (str(age) + "!? That's very old, " + name + "!")
Consider, however, using proper string formatting in the first place:
answer1 = ("{age}!? That's very old, {name}!".format(age=age, name=name))
You have a few options here.
Convert age to string:
answer1 = (str(age) + ...)
Use old-style string formatting:
answer1= "%f!? That's very old, %s!" % (age, name)
Use Python 3.6's excellent string interpolation:
answer1 = f"{age}!? That's very old, {name}!"
I would go with the third.
I am a newbie in this field, and I am trying to solve a problem (not really sure if it is possible actually) where I want to print on the display some information plus some input from the user.
The following works fine:
>>> print (" Hello " + input("tellmeyourname: "))
tellmeyourname: dfsdf
Hello dfsdf
However if I want to assign user's input to a variable, I can't:
>>> print (" Hello ", name = input("tellmeyourname: "))
tellmeyourname: mike
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
print (" Hello ", name = input("tellmeyourname: "))
TypeError: 'name' is an invalid keyword argument for this function
I have researched inside here and other python documentation, tried with %s etc. to solve, without result. I don't want to use it in two lines (first assigning the variable name= input("tellmeyourname:") and then printing).
Is this possible?
Starting from Python 3.8, this will become possible using an assignment expression:
print("Your name is: " + (name := input("Tell me your name: ")))
print("Your name is still: " + name)
Though 'possible' is not the same as 'advisable'...
But in Python <3.8: you can't. Instead, separate your code into two statements:
name = input("Tell me your name: ")
print("Your name is: " + name)
If you often find yourself wanting to use two lines like this, you could make it into a function:
def input_and_print(question):
s = input("{} ".format(question))
print("You entered: {}".format(s))
input_and_print("What is your name?")
Additionally you could have the function return the input s.
no this is not possible. well except something like
x=input("tell me:");print("blah %s"%(x,));
but thats not really one line ... it just looks like it