一切都是通向其他而不是阅读输入

时间:2018-04-27 13:12:40

标签: python

代码:

import math
import time
import os
from random import *

def intro():
    print("Welcome to battle. This is a game where you battle monsters.")
    print("You will try to get the highest score you can.")
    print("You start with 100HP. You will need to survive.")
    print("You get a max revive every 10 battles.")
    print("PS: This game is still in early alpha.")
    print("Bugs are expected.")
    game()

def game():
    health = 100
    revive = 0
    print("You are greeted by a monster...")
    print("1 = Fight")
    print("2 = Take a chance at running")
    choice = input("")
    if choice == 1:
        damage = randint(1,100)
        health = health - damage
        print("You killed the monster!")
        print("But you took "+damage+" damage")
        print("Your new health is: "+health)
    if choice == 2:
        print("You tried to run but failed.")
        damage = randint(70,100)
        health = health - damage
        print("Your new health is: "+health)
    else:
        print("Wrong choice. You died.")
        intro()
intro()

问题:如果您选择使用1,则会导致其他问题。与2.相同。感谢任何有帮助的人! PS:我使用的是Python 3.我不知道这是否重要,我只需要填写这些内容。

2 个答案:

答案 0 :(得分:1)

将您的输入转换为int

<强>实施例

choice = int(input()) 

然后替换

if choice == 2:

elif choice == 2:

根据评论进行编辑

def game():
    health = 100
    revive = 0
    print("You are greeted by a monster...")
    print("1 = Fight")
    print("2 = Take a chance at running")
    choice = int(input(""))
    if choice == 1:
        damage = randint(1,100)
        health = health - damage
        print("You killed the monster!")
        print("But you took "+str(damage)+" damage")     #-->Convert int to str before concatenation 
        print("Your new health is: "+str(health))        #-->Convert int to str before concatenation 
    elif choice == 2:
        print("You tried to run but failed.")
        damage = randint(70,100)
        health = health - damage
        print("Your new health is: "+str(health))       #-->Convert int to str before concatenation 
    else:
        print("Wrong choice. You died.")

答案 1 :(得分:0)

首先需要使用int(input(""))

将输入转换为int

然后:

您需要使用elif choice == 2:代替if choice == 2: