0

I am writing a basic program but when I ask for name and then try and print name while asking for age it gives my this error.

Traceback (most recent call last):
  File "/Users/isabella/Library/Mobile Documents/com~apple~CloudDocs/python/Dt_Programing_V1.py", line 15, in <module>
    age = int(input("Hi",name,"how old are you?" ))
TypeError: input expected at most 1 argument, got 3

This is my code,

age = 0
while age < 5 or age > 98: #Expected ages to be buying a computer
    try:
        age = int(input("Hi", name, "how old are you?"))
    except ValueError:
        print("You need to enter your age correctly, as an intger. ")
        continue

Simple solutions would be much appreciated!

Neuron
  • 5,141
  • 5
  • 38
  • 59
  • Does this answer your question? [Which is the preferred way to concatenate a string in Python?](https://stackoverflow.com/questions/12169839/which-is-the-preferred-way-to-concatenate-a-string-in-python) – esqew Aug 23 '21 at 00:23
  • String concatenation in Python is done with the `+` operator; it’s unclear how you arrived at the conclusion otherwise. – esqew Aug 23 '21 at 00:26
  • @esqew I've seen multiple cases where people look at how `print` arguments work and infer from that that `,` is concatenation. – Carcigenicate Aug 23 '21 at 00:28
  • 1
    If you look at the documentation for the [`print`](https://docs.python.org/3/library/functions.html#print) function. You can see that the first argument is `*objects` (note the asterisk) which means it collects all of the arguments (except the keyword ones) into a list and so you can pass as many strings as you like and it joins them for you. [`input`](https://docs.python.org/3/library/functions.html#input) does not do this and so requires a single argument, so any string concatenation must be done beforehand. It's a difficult distinction for a beginner but the fix is straightforward. – Paul Rooney Aug 23 '21 at 00:38

3 Answers3

1

The input function can only have 1 parameter (or one thing that goes in the parenthesis) so adding commas will not work, instead, you can use + for it to work like so:

age = 0
while age < 5 or age > 98: #Expected ages to be buying a computer:
   try: 
      age = int(input("Hi " + name + " how old are you? " )) 
   except ValueError: 
      print("You need to enter your age correctly, as an intger. ") 
      continue  
1

Unsure if you're confused that you never asked for a name but here is your code fixed.

age = 0

while age < 5 or age > 98: #Expected ages to be buying a computer
    try:
        age = int(input("Hi how old are you?" ))
    except ValueError:
            print("You need to enter your age correctly, as an intger. ")
            continue

Here is your code added the with a name, hoping this is what you meant.

age = 0
name = input("What is your name?")
while age < 5 or age > 98: #Expected ages to be buying a computer
    try:
        age = int(input(f"Hi {name} how old are you?" ))
    except ValueError:
            print("You need to enter your age correctly, as an intger. ")
            continue
0

i don't see your the name part of your code, anyway this is my version of your code

name = input('What is your name?: ')
while True:
    age = input('How Old Are You?: ')
    if not age.isdigit() or not 5 < int(age) < 98:
        print(f'not a valid age, {name}, Try again!\n')
        continue
    break