How to print input when it's a float in Python? [duplicate] - python

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.

Related

I am beginner pls explain diff btw age = "23" print ("happy" + " " + age +"rd birthday") age = 23 print ("happy" + " " + str(age) + "rd birthday" ) [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 12 hours ago.
Improve this question
age = "23"
print ("happy" + " " + age +"rd birthday")
and
age = 23
print ("happy" + " " + str(age) + "rd birthday")
Please explain the difference between the two programs
I cannot find a difference in the output
There is no difference in the output because there is no difference in the input (specifically, the input to the to print call).
In both cases, it receives the string "happy 23rd birthday", the only difference in the code is how you get to that string.
In the first case, you construct it by concatenating strings (including the string "23").
In the second case, you construct it by concatenating strings (one of which is the string "23" converted from the integer 23).
You would also get the same output if you made it a string in any other way, however bizarre the path may be:
val = 4
val = val * (val + 2) - (val // val)
age = str(val)
print ("happy" + " " + age +"rd birthday")
Of course, if you used f-strings, it wouldn't matter so much:
print (f"happy {age}rd birthday")
Except for the fact it might congratulate you on you 21rd birthday, but that's a problem with your current code as well :-)

Question with type() conversion in python 3

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

How do I keep input code on the same line as the print code [duplicate]

This question already has answers here:
Python: "Print" and "Input" in one line [duplicate]
(3 answers)
Closed 2 years ago.
Completely new to coding and this site, so I know this is going to seem like a silly or extremely obvious question. I've tried looking on the site for answers, but I don't know if the answers already given would work on this, or I simply don't know how to input the answers given into my code.
I want the "years old" print to appear right after you input the age, but on the same line.
name = input ("Hello, my name is ")
age = input("I am ")
print ("years old")
job = input("I am a ")
Maybe you want something like this:
name = input("Enter your name : ")
age = input("Enter your age : ")
print(f"My name is {name} and I am {age} years old")
Try something like
name = input ("Name: ")
age = input("Age: ")
print ("Hello, My name is {}, I am {} years old".format(name, age))

How to fix : expected at most 1 argument, got 3 [duplicate]

This question already has answers here:
Error - input expected at most 1 argument, got 3
(2 answers)
Closed 3 years ago.
I am trying to ask the user about their favorite subject, but I get the following error message:
Traceback (most recent call last):
File "C:\Users\BillyG\Documents\Revision\ICT\Challenge 5.py", line 2, in
module
favesub = input("Hello what is your favourite subject", firstname, "?")
TypeError: input expected at most 1 arguments, got 3
The code is:
firstname=input("What is your name: ")
favesub = input("Hello what is your favorite subject", firstname, "?")
print ("I love ", favesub, "aswell")
input expects a single string, so unlike print, where you can append multiple arguments and the string will be parsed as is, you have to format the string yourself. For Python 3.6 and higher, user input(f"Hello what is your favourite subject {firstname}?") or input("Hello what is your favourite subject {}?".format(firstname)) if you're using an older version of Python 3.
input() only takes one argument, but you're providing 3.
Try
input(f"Hello what is your favourite subject {firstname}?")
You can concatenate the strings simply using + while asking for user input in the second line. Currently you are passing it three arguments separated by a comma.
firstname=input("What is your name: ")
favesub = input("Hello what is your favourite subject " + firstname + "?")
print ("I love ", favesub," aswell")
# What is your name: Donald
# Hello what is your favourite subject Donald?Politics
# I love Politics aswell
Try this,
favesub = input("Hello what is your favourite subject"+firstname+"?")
problem here is that input() method can only take atmost one argument, whereas you are passing 1.
firstname = input("What is your name: ")
message = "Hello what is your favourite subject "+firstname+" ?"
favesub = input(message)
print ("I love ",favesub," aswell")

adding two numbers that are in variables

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.

Categories