简体   繁体   中英

How do I write a python program using loops that takes the user input "Years of experience of person 1?", person 2, person 3, and so on?

I recently started learning python. I am trying to write a program using for or while loop that should categorize attendees at an event as "Beginner", "Novice", or "Expert" based on their years of work experience. It first asks the user for the number of attendees and then the years of experience of person 1, 2, 3, and so on up to the total number.

My code is:

n = int(input("Please enter the number of attendees: "))
for i in range(1,n+1,1):
  experience = int(input("Please enter the years of experience of person",i))
  if experience<=2:
    print("Beginner")
  elif 3<=experience<=5:
    print("Novice")
  elif experience>5:
    print("Expert")

There is an error for the variable experience: raw_input() takes from 1 to 2 positional arguments but 3 were given

Any help is appreciated. Thank you.

Use string formatting to substitute the person number into the prompt, you can't put it as a second argument to input() .

experience = int(input(f"Please enter the years of experience of person {i}: "))

in line 3 error experience = int(input("Please enter the years of experience of person",i)) the input function will take just one argument but you gave two arguments argument1="Please enter the years of experience of person" argument2=i to work this you must remove the second argument ie argument2 so the correct input function will be experience = int(input("Please enter the years of experience of person"+str(i)))

n = int(input("Please enter the number of attendees: "))
experience = []
for i in range(0,n):
  exp = int(input(f"Please enter the years of experience of person {i+1}"))
  experience.append(exp)
  if experience[i]<=2:
    print("Beginner")
  elif 3<=experience[i]<=5:
    print("Novice")
  elif experience[i]>5:
    print("Expert")
  

With print statements, you can do the following:

print("hello", 123, "something")

In this case we are passing it three arguments (three things separated by commas between the brackets). The first argument is "hello", the second is 123, etc

And it prints out

hello 123 something

But this is something special that the print function does. The input function doesn't do this. It only takes one argument.

There are a few ways you could fix it, but the way I'd recommend is to replace

"Please enter years of expertise of person", i+1

With

f"Please enter years of experience of person {i+1}"

These are called f strings, if you want to Google more about how they work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM