How do I change a sum for string for it to work?

2024/9/18 22:17:39

This is my whole program I'm working on (with the function not in the right place due to the code needing to be indented) but anyway there is a problem that I'm not sure how to fix.

How do I change it so that this would work along with my program? It says that it is in a string but am not sure how to change this so it calculates the variables from the rest of my program. I am sorry seeming how this is not the only problem I am new to this and am just getting used to how this works.

import time
import randomdef 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 namedef 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 name2def 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,strength1def 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,strength2def difference1(skill1,skill2):
if skill1 >= skill2:result0=skill1-skill2result1=result0/5
elif skill1==skill2:print("There Will Be No Skill Modifier")result1=0
else:result0=skill2-skill1result1=result0/5
return result1def difference2(strength1,strength2):
if strength1 >= strength2:result10=strength1-strength2result2=result10/5
elif strength1==strength52:print("There Will Be No Strength Modifier")result2=0
else:result10=strength2-strength1result2=result10/5
return result2def 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 dicenumber1def 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 dicenumber2welcome()
name=name()
name2=name2()
skill1,strength1=inputtingfor1(name)
skill2,strength2=inputtingfor2(name2)
difference1(skill1,skill2)
difference2(strength1,strength2)
dicenumber1=dicerolling1()
dicenumber2=dicerolling2()
Answer

From the comment thread on the original question, the issue is that skill1 and skill2 are strings, which do not support the - operand. You need to cast them to ints before you perform any mathematical operation on them:

def difference1(skill1,skill2):try:s1 = int(skill1)s2 = int(skill2)except exceptions.ValueError:print("Could not cast to int")return Noneif s1 >= s2:result0 = s1-s2result1=result0/5elif s1==s2:print("There Will Be No Skill Modifier")result1 = 0else:result0=s2-s1result1=result0/5return result1

Depending on what you pass into difference1, you may not be left with an integer. If skill1 and skill2 could be parsed as floats, you'll hit an exception when you try to cast them to ints. If you know this will never be the case, you can remove the try-except block.

https://en.xdnf.cn/q/119822.html

Related Q&A

Convert type str to numerator/ denominator

Im having some trouble converting type str to numbers. I use a separate text-file containing the following numbers 1, 2, 3, 4, 5, 6 and then I import these numbers into python and save them as a list. …

discord.py MemberNotFound exception when passing a real member

When I pass *grant @user add in Discord I get the following exception: Ignoring exception in command grant: Traceback (most recent call last):File "/Users/test/PycharmProjects/slapdash/venv/lib/py…

using argument end= for print function in Python 3.3

Consider the following code:i=0 while i<5:print(i, end=" ")i = i + 1which results in the output:0 1 2 3 4if i want to add a string "the result" before 1 2 3 4, output expected: t…

Get the max value on a list of tuples

I have a list of tuples:card_list= [(2, (1, S)), (0, (12, H)), (1, (5, C)]This list contains cards: (cardindex, (value, suit)) where cardindex is a index to store the position of the card but irrelevan…

Naming of file while saving with extension

How can I save a file with the name as: oldname+"_new"+extension in an elegant way? I currently do: ext = os.path.splitext(file)[1] output_file = (root+/+ os.path.splitext(file)[0]+"_ne…

Unique permutations of a list without repetition

I understand there are many, many posts about permutations (unique, variable length, etc.), but I have not been able to use them to solve my particular issue.Lets say I have a list of metropolitan area…

Join Operation for Dictionary in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How to nest a list based on increasing sequences and ignore left overlapping ranges

This is my input mylist = [2, 7, 8, 11, 7, 9, 10, 15, 22, 30, 32]from 2 to 11, its increasing, so we need to grab the min max [2, 11] from 7 to 10 its increasing, but we need to ignore it because the …

Float comparison (1.0 == 1.0) always false

Im using the following function in Python 2.7.3 and Kivy 1.8.0 to fade-in a Grid widget:def __init__(self, **kwargs):# ...Init parent class here...self.grid.opacity = 0.0Clock.schedule_interval(self.sh…

How to I extract objects? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 2…