如何更改字符串的总和以使其起作用?

时间:2014-03-10 13:57:42

标签: python string python-3.x

这是我正在处理的整个程序(由于代码需要缩进,函数不在正确的位置)但是无论如何都存在一个我不确定如何修复的问题。

如何更改它以便与我的程序一起使用?它说它是一个字符串,但我不知道如何更改它,所以它从我的程序的其余部分计算变量。我很抱歉看到这不是我不熟悉的唯一问题,我只是习惯了它是如何工作的。

import time
import random

def welcome():
ready="no"
while ready=="no":
    print("Welcome To Encounter Simulator Inc. Where We Provide You with combat..")
    print("Readying Start Up Sequence....")
    time.sleep(0.5)
    print("Welcome Are You Ready? Yes or No")
    ready=input(str())
while ready!="yes" and ready!="no":
    print("Yes or No Please")
    ready=input(str())

def name():
areyousure="no"
while areyousure!="yes":
    print("What do you want your 1st character to be named?")
    name=input()
    print("Are You Sure?(yes or no)")
    areyousure=input(str())
if areyousure!="yes" and areyousure!="no":
    print("Yes or No Please")
    areyousure=input(str())
return name

 def name2():
 areyousure2="no"
 while areyousure2!="yes":
     print("What do you want your 2nd character to be named?")
     name2=input()
     print("Are You Sure?(yes or no)")
     areyousure2=input(str())
 if areyousure2!="yes" and areyousure2!="no":
    print("Yes or No Please")
    areyousure2=input(str())
 return name2

 def inputtingfor1(name):
areyousure3="no"
while areyousure3!="yes":
    print("Please Input",name,"'s Attributes..")
    skill1=input("The Skill Attribute= ")
    strength1=input("The Strength Attribute= ")
    print("Are You Sure? (Yes or No)")
    areyousure3=input(str())
return skill1,strength1

def inputtingfor2(name2):
areyousure4="no"
while areyousure4!="yes":
    print("Please Input",name2,"'s Attributes..")
    skill2=input("The Skill Attribute= ")
    strength2=input("The Strength Attribute= ")
    print("Are You Sure (Yes or No)")
    areyousure4=input(str())
return skill2,strength2

def difference1(skill1,skill2):
if skill1 >= skill2:
    result0=skill1-skill2
    result1=result0/5
elif skill1==skill2:
    print("There Will Be No Skill Modifier")
    result1=0
else:
    result0=skill2-skill1
    result1=result0/5
return result1

def difference2(strength1,strength2):
if strength1 >= strength2:
    result10=strength1-strength2
    result2=result10/5
elif strength1==strength52:
    print("There Will Be No Strength Modifier")
    result2=0
else:
    result10=strength2-strength1
    result2=result10/5
return result2

def dicerolling1():
print()
time.sleep(1)
print("This Will Determin Who Gets The Modifiers And Who Loses Them..")
dicenumber1=random.randint(1,6)
print(" The Dice Is Rolling For",name1,)
time.sleep(1)
print("And The Number",name1,"Got Was...",dicenumber1,)
return dicenumber1

def dicerolling2():
print()
time.sleep(1)
print("This Will Determin Who Gets The Modifiers And Who Loses Them..")
dicenumber2=random.randint(1,6)
print(" The Dice Is Rolling For",name2,)
time.sleep(1)
print("And The Number",name2,"Got Was...",dicenumber2,)
return dicenumber2




welcome()
name=name()
name2=name2()
skill1,strength1=inputtingfor1(name)
skill2,strength2=inputtingfor2(name2)
difference1(skill1,skill2)
difference2(strength1,strength2)
dicenumber1=dicerolling1()
dicenumber2=dicerolling2()

2 个答案:

答案 0 :(得分:1)

在原始问题的评论主题中,问题是skill1skill2是字符串,不支持-操作数。在对它们执行任何数学运算之前,需要将它们转换为ints

def difference1(skill1,skill2):
    try:
        s1 = int(skill1)
        s2 = int(skill2)
    except exceptions.ValueError:
        print("Could not cast to int")
        return None

    if s1 >= s2:
        result0 = s1-s2
        result1=result0/5
    elif s1==s2:
        print("There Will Be No Skill Modifier")
        result1 = 0
    else:
        result0=s2-s1
        result1=result0/5
    return result1

根据您传入difference1的内容,您可能不会留下整数。如果skill1skill2可以解析为floats,那么当您尝试将其转换为ints时,您会遇到异常。如果您知道永远不会出现这种情况,可以删除try-except块。

答案 1 :(得分:0)

当技能1 = =技能2时,结果1未定义。

修复:

elif skill1==skill2:
    print("There Will Be No Skill Modifier")
    return 0