What is the mistake in this code ? Why I am not geting the desired output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the mistake in this code ? Why I am not geting the desired output?

import random def ketonattack(): list_g = ["Keton used leaf slash","Keton hit you with a kick ","keton used head butt","keton used sling shot"] l = random.choice(list_g) if l == list_g[0]: print(list_g[0]+" Your monsto got damage of 30%\n [///////]") elif l == list_g[1]: print(list_g[1]+ "Your monsto got Critical damage of 70%\n [///]") elif l == list_g[2]: print(list_g[2]+" Your monsto got damage of 10%\n [////////]") elif l == list_g[3]: print(list_g[3]+" Your monsto dodge the attack\n [//////////]") else: print("error") ketonattack() Output:- Traceback (most recent call last): File "source_file.py", line 4, in <module> l = random.choice(list_g) NameError: name 'list_g' is not defined Process finished with exit code 1.

24th Jan 2021, 8:45 AM
Tanishq Kamble
Tanishq Kamble - avatar
3 Answers
+ 5
local scope variables (i.e. variables inside functions) are not accessible to outside of the function, once the function is called and executed, the variables inside it are destroyed. So you may want to remove that function and make your list_g a normal and global variable. OR I think those nested if-elif-else statement are supposed to be indented inside your function for this to work.
24th Jan 2021, 8:53 AM
noteve
noteve - avatar
+ 2
Your code isn't properly indented, check again to see what is wrong.
24th Jan 2021, 8:54 AM
Abhay
Abhay - avatar
0
Please don't post duplicate questions. You got your answer already https://www.sololearn.com/discuss/2675608/?ref=app
24th Jan 2021, 9:36 AM
Benjamin Jürgens
Benjamin Jürgens - avatar