What is its solution or another way? I wrote a strings list as shown before this I wanted to format it and include options then I wanted to print (by taking input from user) its index but on the terminal error was thrown that 0 or 1 index is not present in list.
options = ["a) 31\n b) 32\n c) 33\n d)34", "a) Energy\n b) Motion\n c) Motion and Energy\n d)Nothing"]
questions = [f"Our brain is consists of ..... bones:\n{options.index(1)}",
f"Physics is the study of .......\n{options.index(2)}"]
q_1 = input(questions[0])
options.index(1)
searches 1
in the list and returns its index. This is not what you seem to want.
You seem to want to get the first element of options
, which is done with options[0]
instead.
It is 0
instead of 1
, because Python lists are indexed starting from 0
, not 1
.
You are doing this correctly when you index into questions
.
I have corrected your code with options[1] insted of options.index(1)
options = ["a) 31\n b) 32\n c) 33\n d)34", "a) Energy\n b) Motion\n c) Motion and Energy\n d)Nothing"]
questions = [f"Our brain is consists of ..... bones:\n{options[0]}", f"Physics is the study of .......\n{options[1]}"]
q_1 = input(questions[0])
The index method searches if the value you provided is in the list ie it options.index(0) checks if an element with value 0 is present and returns its index
To get the element at index 0 use list[0]
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.