+ 1

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

import random def myattack() : ketondamage = ["Keton dodge your attack","30 % damage to keton","10 % damage to keton","Critical 70 % damage to keton"] m = random.choice(ketondamage) print(str(m)) if str(m) == ketondamage[0]: print("[//////////]") elif str(m) == ketondamage[1]: print("[///////]") elif str(m) == ketondamage[2]: print("[/////////]") elif str(m) == ketondamage[3]: print("[///]") else: print("error") myattack() Output :- Traceback (most recent call last): File "source_file.py", line 7, in <module> if str(m) == ketondamage[0]: NameError: name 'm' is not defined Process finished with exit code 1.Traceback (most recent call last): File "v.py", line 7, in <module> if str(m) == ketondamage[0]: NameError: name 'm' is not defined Process finished with exit code 1.

24th Jan 2021, 5:10 AM
Shlok Jathar
Shlok Jathar - avatar
2 odpowiedzi
+ 6
Its python bro . Here everything depends on indention. Cheque your indention of If elif else statement they aren't at proper level See the fixed code \/ import random def myattack() : ketondamage = ["Keton dodge your attack","30 % damage to keton","10 % damage to keton","Critical 70 % damage to keton"] m = random.choice(ketondamage) print(str(m)) if str(m) == ketondamage[0]: print("[//////////]") elif str(m) == ketondamage[1]: print("[///////]") elif str(m) == ketondamage[2]: print("[/////////]") elif str(m) == ketondamage[3]: print("[///]") else: print("error") myattack()
24th Jan 2021, 5:15 AM
Ayush Kumar
Ayush Kumar - avatar
+ 1
Better to use dictionary. keton = { "critical": 0.7, " casual": 0.1, "dodge": 0.3 } def keton_attack(x): a =random.coice(x) print(x[a]) keton_attack(keton)
24th Jan 2021, 6:51 AM
Shadoff
Shadoff - avatar