0

Im new to coding in python, and is trying to make this work.

if input from user is "name age" it works just fine. But I want it to work if user inputs either (name+age) or (name+lastname+age). If I input 3 values I get ValueError: too many values to unpack (expected 2)

and if add name, lastname, age, =map(str, sys.stdin.readline().split()) to the code. I get a not enough values error when user input name+lastname+age

Hopefully someone can help me :)

name, age, =map(str, sys.stdin.readline().split())
age = int(age)

if "Paul" in (name):

    result1 = (age*2)
    print("Answer is", + result1)
1
  • >>> name, lastname, age = map(str, sys.stdin.readline().split()) Name Lastname age >>> name 'Name' >>> lastname 'Lastname' >>> age 'age' It works if you don't have spaces in the name/lastname Commented Jul 6, 2021 at 14:04

3 Answers 3

1

Here's a possibility - read the input line without the map, parse it, and then differentiate the input based on the number of elements in the resulting list,

import sys

entry = sys.stdin.readline()
entry = entry.strip().split()

if len(entry) == 2:
    # Name + age
    name = entry[0]
    age = int(entry[1])
    print(name, age)
elif len(entry) == 3:
    # Name + last name + age 
    name = entry[0]
    last_name = entry[1]
    age = int(entry[2])
    print(name, last_name, age)
else:
    raise ValueError('Wrong input arguments')

if "Paul" in (name):
    result1 = (age*2)
    print("Answer is", + result1)

If the input is nothing of the expected, this code raises an exception. You can instead keep prompting the user until they enter the right values. If you decide to keep the exception approach, consider using a more informative exception message.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of trying to unpack the result of split() into two variables, you can use join() combined with list slicing to join everything but the last value into one (the name):

user = input().split()
age = int(user[-1])
name = ' '.join(user[:-1])

if "Paul" in name:
    print(f"Answer is {age*2}")

This works regardless of how many "words" are in the name:

Paul McCartney 42
Answer is 84
Pauline 42
Answer is 84
Paul W. S. Anderson 42
Answer is 84

2 Comments

This is excatly what i was looking for! Simple and easy to understand for me :D Thanks!
Note also that if you only care about the first name, you don't even need join; just set name, age = user[0], int(user[-1]) to get the first and last list elements and ignore anything that might be in the middle. Easy!
0

You can't split something into a dynamic number of variables this way.

Instead you need to capture the input and then work out how many fields were passed in. Something like:

user = map(str, sys.stdin.readline().split())
lastname = ''
name = user[0]
age = user[1]
if len(user) == 3:
    lastname = user[1]
    age = user[2]

1 Comment

age = user[1] should probably be in another if or else branch. map(str, ...) is not necessary, as sys.stdin.readline().split() is already a list of strings. It's even harmful as the result of map is not a list and [0], etc., can't be used with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.