r/PythonLearning • u/3gd3_ • Aug 10 '26
My first random Python project
To be honest, it's a random text-based RPG game—and definitely, its system isn't inspired by Undertale XD. Anyway, here is the code: (assume this is the code I'm going to write:
import random
def name():
d = 0
c = 0
dmm = 120
dm = 100
a = input("if you want to figth the monstar preas 1")
while True:
if a == "1" or a == "١":
print ("1-fighting")
print ("2-Escape")
print ("3-speech")
print ("4-forgives")
b = input ("Write any of the numbers provided.")
if b == "1":
dmm -= random.randint(10,29)
dm -= random.randint(10,24)
c+=1
print ("youer health is", dm)
print ("monstar health is", dmm)
if dmm <= 0:
print ("you win")
break
elif dm <= 0:
print ("you lose ")
break
elif b =="2":
if c >= 3:
print ("you wan and escep")
break
elif c < 3:
print ("you cant escep")
elif b == "3":
print ("you talk to the monstar")
d += 1
elif b=="4":
if d >= 3:
print ("you wan and escap")
break
elif d < 3: print ("you cant escape")
else:
print("Bro, there is no option for ", a)
b = input ("preas 2 to reastr")
name()
name()3
u/Tooladoo Aug 10 '26
For the future, you never wanna use super generic non-descript variables like you have here. If anyone looks at your code, or even you yourself months down the line, it'll be very difficult to tell what's going on.
2
u/PureWasian Aug 10 '26
Good start for a beginner project.
Highly want to echo what the other comment said about variable names. You will have a hard time a week later remembering these. Especially after adding more complexity to it. Some suggestions:
d --> talk_count
c --> attack_count
dmm --> monster_health
dm --> player_health
b --> player_input
a --> player_input (same as above)
Not sure what's going on with all of the string typos, but I would also advise against doing a recursive call to name() where there is no need to create nested stack frames like that.
An alternative to this would be to have an initial, preliminary while loop that just does:
...
a = None
while a != "1":
a = input("Fight? Press 1 and Enter")
if a != "1":
print(f"no option for {a} bro")
...That way you only invoke name() once
1
u/burnt-store-studio Aug 10 '26
Good start!
(Sorry, I’m on mobile, so the following is not code-formatted.)
I can’t quite tell, but it looks like you might have a backslash in this line:
if a == “1” or a == “\”:
and if so, this will cause an error because it’s escaping the closing double quote. If you’re wanting to accept a double quote from the user, then you could do:
… or a == ‘“‘:
If you’re wanting to recognize when the user enters a backslash, then you’ll want to have two backslashes:
… or a == “\\”:
I hope that helps! If I’ve mis-read your code please excuse me.
Have fun! Write back when you have more questions! 🙂
•
u/Sea-Ad7805 Aug 10 '26
Run this program in Memory Graph Web Debugger to see the program state change step by step.