Two arguments in input function
|
Two arguments in input function
|
|
Nov-08-2025, 11:34 AM
Please do not upload images of code/data/errors.
copy/paste as text and properly format using BBCode tags.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
Nov-08-2025, 12:35 PM
As per Python's official documentation the
input() function accepts a single argument. You need to create a string containing "Give the quotation of Math :", then feed input() with that argument.
« We can solve any problem by introducing an extra level of indirection »
Nov-08-2025, 01:00 PM
Hi,
generally speaking, you certainly want to learn more about Python's string formating. Leaving the error on the input function aside, the string formatting on the print function isn't the way to do it either. See https://docs.python.org/3/tutorial/input...formatting for details.Regards, noisefloor
Nov-09-2025, 12:56 AM
You should read up a bit on Python f-strings, for example here.
course = 'English'
grade = input('Give the quotation of ', course)The above give this error:Quote:Traceback (most recent call last): You can fix this error: # can fix the above using a so-called f-string
# input always returns a string
course = 'English'
grade = input(f'Give the quotation of {course}') # enter a number like 11
print(f'grade for {course} = {grade} from 20')
print(f'grade is type {type(grade)}')type(var) tells you what type of variable var is. The output of input() is always <class 'str'>, a string variable.courses = ['English', 'Deutsch', 'Castellano', '中文', 'Python']
# shouldn't really give yourself grades
# I got these grades fair and square! Honest guv!
my_grades = ['19', '18', '17', '16', '1']
# use a so-called f-string to put values in a string
for index, course in enumerate(courses):
print(f'index = {index}, course = {courses[index]}, my grade = {int(my_grades[index]) / 20}')Using .split() you can get multiple entries from 1 input() By default .split() splits a string on spaces, but you can tell split() to split on any character. Here I chose , # input only gives you a string value back
# to get multiple entries from input() you can use .split()
# then names will be a list split() and you get back a list
string = input('Enter the surnames separated by a , ') # enter something like: Jones,Schmidt,Fernández,朱
type(string) # returns <class 'str'>
names = input('Enter the surnames separated by a , ').split(',')
type(names) # returns <class 'list'>
|
|
|
Users browsing this thread: 1 Guest(s)
