英文:
How can I fix this issue with my python text-based game?
问题
以下是您提供的代码的翻译部分:
import os
import random
battle = True
nameA = "John"
nameB = "Marcus"
maxhpA = 50
hpA = 50 # 生命值
atkA = 5 # 攻击伤害
potA = 10 # 药水数量
luckA = 30
goldA = 0 # 玩家金币数量
maxhpB = 50
hpB = 50 # 生命值
atkB = 5 # 攻击伤害
potB = 10 # 药水数量
luckB = 30
goldB = 0 # 玩家金币数量
def draw():
print("XX-----------------------XX")
def clear():
os.system("cls")
clear()
def stop():
input(">")
def healA(amount_healed):
global hpA, maxhpA, nameA
if hpA + amount_healed < maxhpA:
hpA += amount_healed
else:
hpA = maxhpA
print(nameA + "'s HP已恢复至" + str(hpA) + "!")
def healB(amount_healed):
global hpB, maxhpB, nameB
if hpB + amount_healed < maxhpB:
hpB += amount_healed
else:
hpB = maxhpB
print(nameB + "'s HP已恢复至" + str(hpB) + "!")
def turnA():
global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB
choice = input(nameA + "'s回合>")
if choice == "1":
hpB -= atkA
print(nameA + "攻击了!")
print(nameB + "受到" + str(atkA) + "伤害!")
stop()
elif choice == "2":
if random.randint(1, 100) < int(luckA):
hpB -= 2 * atkA
print()
print(nameA + "进行了特殊攻击!")
print(nameB + "受到" + str(2 * atkA) + "伤害!")
stop()
else:
print(nameA + "未命中特殊攻击!")
stop()
elif choice == "3":
if potA > 0:
potA -= 1
print(nameA + "使用了药水。")
healA(25)
input(">")
else:
print("没有药水!")
stop()
else:
print("无效选项!")
turnA()
clear()
stats()
stop()
def turnB():
global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB
choice = input(nameB + "'s回合>")
if choice == "1":
hpA -= atkB
print(nameB + "攻击了!")
print(nameA + "受到" + str(atkB) + "伤害!")
stop()
elif choice == "2":
if random.randint(1, 100) < int(luckB):
hpA -= 2 * atkB
print()
print(nameB + "进行了特殊攻击!")
print(nameA + "受到" + str(2 * atkB) + "伤害!")
stop()
else:
print(nameB + "未命中特殊攻击!")
stop()
elif choice == "3":
if potB > 0:
potB -= 1
print(nameB + "使用了药水。")
healB(25)
stop()
else:
print("没有药水!")
stop()
else:
print("无效选项!")
turnB()
clear()
stats()
stop()
def stats():
print("名称: {0:15}{1}".format(str(nameA), str(nameB)))
print("生命值: {0:15}{1}".format(str(hpA), str(hpB)))
print("攻击伤害: {0:15}{1}".format(str(atkA), str(atkB)))
print("药水数量: {0:15}{1}".format(str(potA), str(potB)))
print("幸运值: {0:15}{1}".format(str(luckA), str(luckB)))
draw()
print("1 - 攻击")
print("2 - 冒险攻击")
print("3 - 使用药水 (25HP)")
draw()
while battle:
stats()
turnA()
clear()
stats()
turnB()
clear()
希望这能帮助您理解代码,并找到问题所在。如果您有任何其他问题,请随时提出。
英文:
import os
import random
battle = True
nameA = "John"
nameB = "Marcus"
maxhpA = 50
hpA =50 #amount of health
atkA = 5 #attack damage
potA = 10 #potion number
luckA = 30
goldA = 0 #amount of player_gold
maxhpB = 50
hpB =50 #amount of health
atkB = 5 #attack damage
potB = 10 #potion number
luckB = 30
goldB = 0 #amount of player_gold
def draw():
print("XX-----------------------XX")
def clear():
os.system("cls")
clear()
def stop():
input(">")
def healA(amount_healed):
global hpA, maxhpA, nameA
if hpA + amount_healed < maxhpA:
hpA += amount_healed
else:
hpA = maxhpA
print(nameA + "'s HP refilled to " + str(hpA) + "!")
def healB(amount_healed):
global hpB, maxhpB, nameB
if hpB + amount_healed < maxhpB:
hpB += amount_healed
else:
hpB = maxhpB
print(nameB + "'s HP refilled to " + str(hpB) + "!")
def turnA():
global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB
choice = input( nameA + "'s turn >")
if choice == "1":
hpB -= atkA
print(nameA +" attacked!")
print(nameB + " took " + str(atkA) + " damage!")
stop()
if choice == "2":
if random.randint(1,100) < int(luckA):
hpB -= 2*atkA
print()
print(nameA +" preformed a special attack!!")
print(nameB + " took " + str(2*atkA) + " damage!")
stop()
else:
print(nameA + " missed their special attack!")
stop()
if choice == "3":
if potA > 0:
potA -= 1
print(nameA +" used a potion.")
healA(25)
input(">")
else:
print("No potions!")
stop()
else: #issue
print("Invalid option!")
turnB()
clear()
stats()
stop()
def turnB():
global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB
choice = input( nameB + "'s turn >")
if choice == "1":
hpA -= atkB
print(nameB +" attacked!")
print(nameA + " took " + str(atkB) + " damage!")
stop()
if choice == "2":
if random.randint(1,100) < int(luckB):
hpA -= 2*atkB
print()
print(nameB +" preformed a special attack!!")
print(nameA + " took " + str(2*atkB) + " damage!")
stop()
else:
print(nameB + " missed their special attack!")
stop()
if choice == "3":
if potB > 0:
potB -= 1
print(nameB +" used a potion.")
healB(25)
stop()
else:
print("No potions!")
stop()
else: # issue is here!
print("Invalid option!")
turnB()
clear()
stats()
stop()
def stats():
print("Name: {0:15}{1}".format(str(nameA), str(nameB)))
print("HP: {0:15}{1}".format(str(hpA), str(hpB)))
print("ATK: {0:15}{1}".format(str(atkA), str(atkB)))
print("POTIONS: {0:15}{1}".format(str(potA), str(potB)))
print("LUCK: {0:15}{1}".format(str(luckA), str(luckB)))
draw()
print("1 - ATTACK")
print("2 - RISKY ATTACK")
print("3 - USE POTION (25HP)")
draw()
while battle:
stats()
turnA()
clear()
stats()
turnB()
clear()
Hello. This is a python text-based game I have been working on. It is very simple but there is an error I just cannot figure out. The game works very simply, player A has their turn, and then player B gets theirs, to be expected.
All the options work fine, but I am getting an error with the "else" parts of the turnA and turnB functions. If you play a move that's invalid, I want the player to have another chance to make their move, but it is causing issues as seen in the image below. every second input is now counted as an invalid input.
答案1
得分: 1
你当前的代码看起来类似于这样:
if choice == "1":
# 做某事
if choice == "2":
# 做某事
if choice == "3":
# 做某事
else:
# 做某事
使用这个语法,当choice
不是选项3时,else
语句会执行。相反,你应该使用:
if choice == "1":
# 做某事
elif choice == "2":
# 做某事
elif choice == "3":
# 做某事
else:
# 做某事
这将依次检查每个选项,然后在没有匹配任何choice
选项时运行else
条件。
此外,你可以阅读一下关于Python类的内容。这个项目将是一个很好的方式来介绍你自己一些面向对象编程中的新概念,比如在对象中存储属性,比如HP。
英文:
Your current code looks similar to this:
if choice == "1":
# Do something
if choice == "2":
# Do something
if choice == "3":
# Do something
else:
# Do something
With this syntax, your else statement is applying when the choice is not Option 3. Instead, you should use:
if choice == "1":
# Do something
elif choice == "2":
# Do something
elif choice == "3":
# Do something
else:
# Do something
This will step through each choice and then run the else condition if none of the choice
options match.
Also whilst you're here, have a read around Python Classes. This project will be a good way to introduce yourself to some new concepts within object oriented programming e.g. storing properties such as HP within objects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论