Write a function that returns the type of clothing you should wear, based on the parameter temperature. If the temperature is below -10, you will wear a parka and toque (return “parka and toque”). If the temperature is between -10 and 0, wear a toque (return “toque”). If the temperature is greater than 0 but less than 10, wear a sweater (return “sweater”). If the temperature is between 10 and 20, wear a t-shirt (return “t-shirt”). If the temperature is greater than 20, wear shorts (return “shorts”).
For example:
wear_the_right_thing(25) == "shorts"
wear_the_right_thing(-25) == "parka and toque"
wear_the_right_thing(-5) == "toque"
here is my code:
def wear_the_right_thing(temperature):
if temperature < -10:
return "parka and toque"
if temperature >= -10:
return "toque"
if temperature > -10 and temerature <= 0:
return "sweater"
if temperature > 10 and temperature <= 20:
return "t-shrit"
if temperature > 20:
return "shorts"
Here is my result (not the output this just my marked):
Result Actual Value Expected Value Notes
Fail 'toque' 'shorts' wear_the_right_thing(25)
Fail 'toque' 't-shirt' wear_the_right_thing(20)
Fail 'toque' 't-shirt' wear_the_right_thing(15)
Fail 'toque' 't-shirt' wear_the_right_thing(10)
Fail 'toque' 'sweater' wear_the_right_thing(9)
Fail 'toque' 'sweater' wear_the_right_thing(1)
Pass 'toque' 'toque' wear_the_right_thing(0)
Pass 'toque' 'toque' wear_the_right_thing(-10)
Pass 'parka and toque' 'parka and toque' wear_the_right_thing(-11)
Pass 'parka and toque' 'parka and toque' wear_the_right_thing(-30)
You passed: 40.0% of the tests
so i fail the test so can help me to get 100 thank you very much
You have over lapping conditions. "toque" is always reported if temperature is above -10. You also have spelling mistakes, e.g. "temperature".
def wear_the_right_thing(temperature):
if temperature < -10:
return "parka and toque"
elif temperature >= -10 and temperature <= -5:
return "toque"
elif temperature > -10 and temperature <= 0:
return "sweater"
elif temperature > 0 and temperature <= 20:
return "t-shrit"
elif temperature > 20:
return "shorts"
You need to use the and to give multiple condition in one if statement.
Example
if temperature > -10 and temperature < 0 :
return "toque"
There are many issues with your conditions, try something like this
def wear_the_right_thing(temperature):
if temperature < -10:
return "parka and toque"
elif -10 <= temperature <= -5:
return "toque"
elif -5 < temperature <= 0:
return "sweater"
elif 0 < temperature <= 20:
return "t-shrit"
elif temperature > 20:
return "shorts"
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 days ago.
Improve this question
I'm writing a class for a project in my beginners python class, and I'm getting stuck on a certain part. The rest of my project is done, but I can't figure out this one little thing. I have an iteration variable that goes through my dictionary with automatically set values, starting at -1,0,1, etc. to a value specified by the user. Another function does some math and assigns each number after 0 to a number based on my variable self.horsepower, chosen by the user, all the way to self.maxGear, a number also chosen by the user. The self.speed variable gets set in the transmission function. It should just be assigning a number based on the iteration variable from the for loop above, but it gets set to 0 everytime.
I expected it to set the gear value associated in the dictionary to the max speed if it is greater than the number in the gear value speed. I think I need to incorporate the current gear, but I just don't know how.`
My issue is the transmission function. Specifically line 92
class Car:
def __init__(self, engine, chassis, drivetrain, turbo, horsepower, maxGear):
self.engine = engine
self.chassis = chassis
valid_drivetrains = ['AWD','FWD','RWD']
if drivetrain in valid_drivetrains:
self.drivetrain = drivetrain
else:
self.drivetrain = 'AWD'
self.turbo = turbo
self.horsepower = horsepower
self.wheelsize = ""
self.handbrake = True
self.speed = 0.0
self.maxGear = maxGear
self.currentGear = 0
self.gearlist = [-1]
self.topSpeedList = {-1: 20, 0: 0}
"""
_engine (str) -- String represnting engine (User chosen)
_chassis (str) -- String represnting chassis (User chosen)
_drivetrain (str) -- String represnting drivetrain. Defaults to AWD if invalid drivetrain is chosen (User chosen)
_turbo (bool) -- Boolean represnting if there is a turbo or not (User chosen)
_horsepower (int) -- Integer represnting the horsepower of the vehicle (User chosen)
_wheeltype (str) -- String represnting the automatically chosen wheel type based on the horsepower of the vehicle
_handbrake (bool) -- Boolean represnting the handbrake and if its pulled or not. Sets speed to 0 (User chosen)
_speed (float) -- Float represnting the current speed of the vehicle.
_maxGear (int) -- A bunch of integers from -1 to maxGear designated by the user. (User chosen)
_currentGear (int) The current gear the user is in, from -1 to _maxGear. (User chosen)
_throttle (int) A number from 0-100 (User chosen)
"""
# IDENTIFIERS:
def all_gears_identifier(self):
"""
maxGear: An integer designated by the user, integral for calculating all the available gears.
This function takes in all numbers from 0 to maxGear and appends them to the gearlist, making them available gears.
"""
for g in range(self.maxGear + 1):
self.gearlist.append(g)
return(self.gearlist)
def gear_identifier(self):
if self.currentGear == -1:
print("You are in reverse.")
elif self.currentGear == 0:
print("You are in neutral.")
elif self.currentGear == 1:
print("You are in "+str(self.currentGear)+"st gear")
elif self.currentGear == 2:
print("You are in "+str(self.currentGear)+"nd gear")
elif self.currentGear == 3:
print("You are in "+str(self.currentGear)+"rd gear")
elif self.currentGear >= 4:
print("You are in "+str(self.currentGear)+"th gear")
def wheel_size_identifier(self, horsepower):
"""
horsepower: Integer, essential for calculations in deterining the wheel size
This function sets the value of the string wheelsize to a predetermined value.
"""
if horsepower < 150:
self.wheeltype = "What are you driving (TEMPORARY!)"
if horsepower < 315:
self.wheeltype = "Economy (TEMPORARY!)"
if horsepower < 500:
self.wheeltype = "Sport (TEMPORARY!)"
def PSI(self, turbo, horsepower, throttle):
"""
turbo: Boolean, returns the PSI (0 or real PSI)
horsepower: Integer, essential to calculating the PSI
throttle: Integer, essential to calculating the PSI
"""
if turbo == True:
x = horsepower / 800
x **= (1/2)
x *= (throttle / 3)
x = str(round(x, 1))
print((x+" PSI"))
else:
print("0.0 PSI")
# NUM MODIFIERS:
def transmission(self):
"""
Takes some parts from the gas and brake functions and converts them into use for getting top speeds of each gear.
Sets a gear:topSpeed to each top gear to a gear starting from 1 to maxGear and then returns this dictionary.
"""
x = (self.horsepower * .036)*(10 ** .5)
for i in range(1, self.maxGear + 1):
self.topSpeedList[i] = round((x*i*0.5),1)
for i in range(-1, self.maxGear + 1):
if self.speed > self.topSpeedList[self.currentGear]:
#self.speed = self.topSpeedList[i] this is the issue
self.speed = self.topSpeedList[i]
"""the way I thought this worked is that from the automatically set values in the dictioanry,
if the speed is greater than d[gear], it sets the speed to the value assocatiaed with d[gear]"""
else:
self.speed = self.speed
#print(self.topSpeedList[i])
return(self.topSpeedList)
def gas(self, throttle):
"""
Takes in THROTTLE, a number from 0-100. Essential for calculating the speed increase.
"""
Car.transmission(self)
if throttle > 100:
throttle = 100
elif throttle < 0:
throttle = 0
x = self.horsepower * .03
self.speed = x * (throttle * .22)
self.speed = round(self.speed, 3)
Car.transmission(self)
def brake(self, throttle):
"""
Takes in THROTTLE, a number from 0-100. Essential for calculating the speed decrease.
"""
if throttle > 100:
throttle = 100
elif throttle < 0:
throttle = 0
x = self.horsepower * .03
x *= (throttle * .11)
self.speed -= x
self.speed = round(self.speed, 3)
if self.speed < 0:
self.speed = 0
def gearshift(self, shift):
"""Adds or subtracts gear number via string: - or +"""
if self.currentGear >= self.maxGear:
self.currentGear = self.maxGear
elif shift == '+':
self.currentGear += 1
print("Shifted one gear up.")
elif shift == '-':
self.currentGear -= 1
print("Shifted one gear down.")
else:
print("Invalid gear. Please input '+' or '-', thank you.")
if __name__=="__main__":
carA = Car("13B", "RX7 Chassis", "RWD", True, 400, 6)
carA.gearshift("-")
carA.gas(100)
for i in range(30,100,10):
carA.gearshift("+")
print(carA.currentGear,"gear")
carA.gas(i)
print(carA.speed,"mph")
#Last thing to do is to implement the speed caps via gearing.
I am writing a function that checks if two numbers in a list are within a certain range and then print various statements if one number is in the range or if both numbers are in the range. My problem is that the right statements aren't being printed under the right conditions despite the statements looking fine.
def qualitative_reading(input_list):
humidity = 0
temp = 0
humid_round= round(input_list[0])
temp_round= round(input_list[1])
if humid_round in range(39, 61, 1):
humidity = 1
else:
humidity = 0
if temp_round in range(67, 75, 1):
temp = 1
else:
temp = 0
if humidity + temp ==2:
print('The room is comfortable')
elif temp == 1 & humidity == 0:
print("Ideal conditions for bacteria and virus")
print('The room is not comfortable to be in')
elif temp == 0 & humidity == 1:
print('The room is not comfortable to be in')
print('Energy being wasted')
elif humidity + temp ==0:
print('The room is not comfortable to be in')
You can try something like this instead
def qualitative_reading(input_list):
humidity = False
temp = False
humid_round= input_list[0]
temp_round= input_list[1]
humidity = 39 <= humid_round < 61
temp = 67 <= temp_round < 75
if humidity and temp:
print('The room is comfortable')
elif temp and not humidity:
print("Ideal conditions for bacteria and virus")
print('The room is not comfortable to be in')
elif not temp and humidity:
print('The room is not comfortable to be in')
print('Energy being wasted')
elif not humidity and not temp:
print('The room is not comfortable to be in')
I suspect that the problem lies with the fact that you are using & but python uses the keyword and.
Some of the other things I noticed were:
The use of range is not very applicable here so you can use an if statement instead.
humidity and temp seem to only have two values, so they can be turned into booleans.
I have been tasked with producing a simple program that simulates the actions of a vehicle from a list of command stored & accessed within a text file.
The expected output would look something like this;
Loading simulation...
Accelerating...
Speed = 5, Gear = 1, Direction = 0
Accelerating...
Speed = 10, Gear = 1, Direction = 0
Accelerating...
Changing up...
Current gear = 2
Speed = 15, Gear = 2, Direction = 0
Accelerating...
Speed = 20, Gear = 2, Direction = 0
Accelerating...
Yet my output looks like this;
Loading Simulation....
Car Gear is First (1)
Accelerating...
Car Gear is First (1)
Speed = 5, Gear = 1, Direction = [-1, 0, 1]
Braking...
Car Gear is First (1)
Speed = 0, Gear = 1, Direction = [-1, 0, 1]
Car Gear is First (1)
Accelerating...
Car Gear is First (1)
Speed = 5, Gear = 1, Direction = [-1, 0, 1]
Braking...
Car Gear is First (1)
Speed = 0, Gear = 1, Direction = [-1, 0, 1]
Car Gear is First (1)
When I run my code in Idle, i get no errors of any kind, when i use Thonny, it gives me the folloing error when it analyses the code:
Line 113 : Either all return statements in a function should return an expression, or none of them should.
Line 113 : Unused argument 'selected_gear'
Below is a copy of my code also:
RIGHT = 1
LEFT = -1
FORWARD = 1
REVERSE = 0
STRAIGHT = 0
#gears and allowable speeds are as follows:
#zero (reverse) (speed -1 to -10). Max reverse speed of car is -10
#one (speed 0 to 10)
#two (speed 10 to 20)
#three (speed 20 to 30)
#four (speed 30 to 45)
#five (speed 45 to 80). Max speed of car is 80
#gears change automatically, one gear at a time
#direction values are similar to numbers on clock face
#0 = 12 = straight on. All other directions = 1-11
class Car:
def __init__(self):
self.speed = 0
self.gear = [0,1,2,3,4,5]
self.direction = [-1,0,1]
self.broken = False #indicates whether car is broken
self.simulation = []
self.simulation_loaded = False
def accelerate(self):
if self.broken:
print("Car is broken!")
return
print("Accelerating...")
if self.gear == REVERSE:
self.speed -= 5
else:
self.speed += 5
if self.speed > 80:
self.speed = 80
if self.speed < -10:
self.speed = -10
self.change_gear(self.gear)
self.display_stats()
def brake(self):
if self.broken:
print("Car is broken...")
return
print("Braking...")
if self.speed < 0:
self.speed += 5
if self.speed > 0:
self.speed = 0
elif self.speed > 0:
self.speed -= 5
if self.speed < 0:
self.speed = 0
self.change_gear(self.gear)
self.display_stats()
def turn_steering_wheel(self, direction_change):
if self.broken:
print("Car is broken...")
return
if self.gear == REVERSE:
print ("Car is in reverse...")
if direction_change == RIGHT:
self.direction = -1
print("Reversing Right")
elif direction_change == REVERSE:
self.direction = 12
print("Reversing")
elif direction_change == LEFT:
self.direction = 1
print("Reversing Left")
elif self.gear == FORWARD:
if direction_change == LEFT:
self.direction = -1
print("Turning Left")
elif direction_change == STRAIGHT:
self.direction = 0
print("Moving Forward")
elif direction_change == RIGHT:
self.direction = 1
print("Turning Right")
self.display_stats()
def change_gear(self, selected_gear = FORWARD):
if self.broken:
print("Car is broken...")
return self.broken
# if self.gear == 0 and self.speed >= 0:
# print("you are going forward while in reverse gear...")
# return self.broken
# elif self.gear >= 1 and self.speed <= -1:
# print("you are going reverse while in foward gear...")
# return self.broken
if self.speed <= -1:
self.gear = 0
print("Car Gear is Neutral (0)")
return self.gear
elif self.speed <= 10:
self.gear = 1
print("Car Gear is First (1)")
return
elif self.speed <= 20:
self.gear = 2
print("Car Gear is Second (2)")
return
elif self.speed <= 30:
self.gear = 3
print("Car Gear is Third (3)")
return
elif self.speed <= 40:
self.gear = 4
print("Car Gear is Fourth (4)")
return
elif self.speed <= 50:
self.gear = 5
print("Car Gear is Fifth (5)")
return
self.display_stats()
self.change_gear(self.gear)
#check to see if car is going forward when reverse is selected and vice versa
#work out what gear you need to be in based on car’s speed
#Loop one gear at a time, either changing up or down, to get to required gear
print("Changing up...")
def display_stats(self):
print(f"Speed = {self.speed}, Gear = {self.gear}, Direction = {self.direction}")
def load_simulation(self, filename):
file = open(filename)
line = file.readline()
while line !='':
self.simulation.append(line.strip())
line = file.readline()
file.close()
self.simulation_loaded = True
return self.simulation
def run_simulation(self):
if self.simulation_loaded == False:
print("Error - Simulation.txt file is present")
else:
print("Loading Simulation....")
for action in self.simulation:
if action == "FORWARD":
self.change_gear (FORWARD)
elif action == "ACCELERATE":
self.accelerate()
elif action == "LEFT":
#self.direction(LEFT)
print("TURNING LEFT")
elif action == "RIGHT":
#self.direction(RIGHT)
print("TURNING RIGHT")
'''***WHEN USING SELF.DIRECTION(LEFT)/(RIGHT) the following error is given:
Traceback (most recent call last):
File "C:\Users\lenovo\Desktop\Bsc Computer Forensics - Laptop\Software-Dev\car-temp-2.py", line 207, in <module>
my_car.run_simulation()
File "C:\Users\lenovo\Desktop\Bsc Computer Forensics - Laptop\Software-Dev\car-temp-2.py", line 183, in run_simulation
self.direction(LEFT)
TypeError: 'list' object is not callable*** '''
elif action == "BRAKE":
self.brake()
else:
self.change_gear (REVERSE)
if __name__ == '__main__':
my_car = Car()
my_car.load_simulation("simulation.txt")
my_car.run_simulation()
Would I please be able to ask if anyone could explain what the errors im getting mean and where in my code I need to be looking, its got me feeling a little lost now - I've tried to research them, fix them but anyhting I try either seems to have no impact or gives errors that otherwise don't exist.
The errors occur in your change_gear() function, although they are more like warnings and should not pose serious problems:
Line 113 : Either all return statements in a function should return an expression, or none of them should.
The first two return statements return a value while the others don't. This is inconsistent and makes the code harder to understand. Since you don't actually use the returned values anywhere, you can remove them (i.e. use plain return).
Line 113 : Unused argument 'selected_gear'
You don't use the selected_gear argument anywhere inside change_gear(). You can remove it to get rid of the warning/error.
So I have this simple code for a class CoffeeMachine.
The question is: Is there a way to simplify def buy so that instead of subtracting values from each parameter, the value of all parameters will be updated at once? something like
return CoffeeMachine(- 250, - 0, - 16, - 1, + 4)
instead of:
self.water -= 250
self.beans -= 16
self.money += 4
self.cups -= 1
the code is bellow (redundant functions deleted so it will be easier to read)
class CoffeeMachine:
def __init__(self, water, milk, beans, cups, money):
self.water = water
self.milk = milk
self.beans = beans
self.cups = cups
self.money = money
def buy(self):
coffee_type = input('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:')
if coffee_type == '1':
if self.water <= 250:
print('Sorry, not enough water!')
elif self.beans <= 16:
print('Sorry, not enough beans!')
else:
print('I have enough resources, making you a coffee!')
self.water -= 250
self.beans -= 16
self.money += 4
self.cups -= 1
elif coffee_type == '2':
if self.water <= 350:
print('Sorry, not enough water!')
elif self.milk <= 75:
print('Sorry, not enough milk!')
elif self.beans <= 20:
print('Sorry, not enough beans!')
else:
self.water -= 350
self.milk -= 75
self.beans -= 20
self.money += 7
self.cups -= 1
print('I have enough resources, making you a coffee!')
elif coffee_type == '3':
if self.water <= 200:
print('Sorry, not enough water!')
elif self.milk <= 100:
print('Sorry, not enough milk!')
elif self.beans <= 12:
print('Sorry, not enough beans!')
else:
self.water -= 200
self.milk -= 100
self.beans -= 12
self.money += 6
self.cups -= 1
print('I have enough resources, making you a coffee!')
elif coffee_type == 'back':
pass
Coffee_Machine = CoffeeMachine(400, 540, 120, 9, 550)
Coffee_Machine.power_on()
Class attributes are the variables in a class whose value is equal to all the objects/instances of that class. We can change the value of class attribute by class name.
Lets look at the below class x
class x:
a=7
def __init__(self,z):
self.b=z
def sum(self):
return(self.a+self.b)
e=x(3)
f=x(4)
print("Initial sum\n")
print(e.sum(),f.sum())
print("\n")
x.a=5
print("Class attribute has been changed\n")
print("After changing class attribute\n")
print(e.sum(),f.sum())
Output:
Initial sum
10 11
Class attribute has been changed
After changing class attribute
8 9
You could split your buy method logic in different methods that attempt to buy the specific coffee type. And then create a new method that updates the values (which assumes that the values were sufficient):
def buy(self):
coffee_type = input('What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:')
if coffee_type == '1':
self.attempt_to_buy_espresso()
# ...
def attempt_to_buy_espresso(self):
price = 4
required_water = 250
required_beans = 16
if self.water <= required_water:
print('Sorry, not enough water!')
elif self.beans <= required_beans:
print('Sorry, not enough beans!')
else:
print('I have enough resources, making you a coffee!')
self.prepare_drink(price=price, water=required_water, beans=required_beans)
def prepare_drink(self, price, water=0, beans=0, milk=0)
self.money += price
self.water -= water
self.beans -= beans
self.milk -= milk
self.cups -= 1
You could improve this further, e.g. by storing the required water, beans and milk values for each drink in an unified data structure (for example a dictionary) so that they are not spread across different methods, or separate the logic of preparing the drink and updating the money value, since those represent separate actions.
This task is to determine the difference between two attributes, strength and skill, from game characters. The process for this is:
Determining the difference between the strength attributes.
The difference is divided by five and rounded down to create a ‘strength modifier’.
The same process is carried out for the skill attribute and named ‘skill modifier’.
Then the game begins:
Both players throw a six sided die.
If the values are the same, no changes happen.
If the values are different, the player with the highest value then gains the strength and skill worked out earlier and it is then added to their total. The player who has the lowest value has the strength and skill worked out taken away from their total.
This must repeat until the strength attribute is equal or below 0, however, this part I cant do, I know how to loop but this is always done by asking the user if they wish to go again.
c1sc=random.randint(1,6)
c2sc=random.randint(1,6)
if c1sc > c2sc:
c1st=c1st+strengthmodifier
c2st=c2st-strengthmodifier
c1sk=c1sk+skillmodifier
c2sk=c2sk-skillmodifier
print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
elif c2sc > c1sc:
c1st=c1st-strengthmodifier
c2st=c2st+strengthmodifier
c1sk=c1sk-skillmodifier
c2sk=c2sk+skillmodifier
print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
else:
print('It\'s a draw, no changes were made.')
if c1sk < 0:
c1sk=0
elif c2sk < 0:
c2sk=0
if c1st < 0:
print('Character 1 died. Game over.')
elif c2st < 0:
print('Character 2 died. Game over.')
Please help!
Let me introduce you to the while loop. You use this when you want something to occur if the specified condition is true or false.
I briefly edited your code.
import random
def main():
player1=20
player2=12
strength=(20-10)/5
strength_mod=int(strength)
while player1>0 and player2>0:
dice1=random.randint(1,6)
dice2=random.randint(1,6)
if dice1>dice2:
player1+=strength_mod
player2-=strength_mod
print("Player 1 strength is: ", player1)
print("Player 2 strength is: ", player2)
elif dice1<dice2:
player2+=strength_mod
player1-=strength_mod
print("Player 1 strength is: ", player1)
print("Player 2 strength is: ", player2)
main()
I gave players 1 and 2 initial values bet these can be changed to your liking. To round down, I made another variable and changed strength to an integer.
Just so you get the picture: (in pseudocode)
while (c2sk > 0 && c1sk > 0)
//do complete everything up until the if statements
if c1st <0
print "c1st died"
elif c2st <0
print "c2st died"
Not sure if this is what you are looking for exactly. If the c2sk and c1sk is supposed to be randomly reassigned each time it should be inside the loop.
Add another loop to do the reductions...
c1sc=random.randint(1,6)
c2sc=random.randint(1,6)
if c1sc > c2sc:
while c2st > 0 and c2sk > 0:
c1st=c1st+strengthmodifier
c2st=c2st-strengthmodifier
c1sk=c1sk+skillmodifier
c2sk=c2sk-skillmodifier
print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
elif c2sc > c1sc:
while c1st > 0 and c1sk > 0:
c1st=c1st-strengthmodifier
c2st=c2st+strengthmodifier
c1sk=c1sk-skillmodifier
c2sk=c2sk+skillmodifier
print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
else:
print('It\'s a draw, no changes were made.')
if c1sk < 0:
c1sk=0
elif c2sk < 0:
c2sk=0
if c1st < 0:
print('Character 1 died. Game over.')
elif c2st < 0:
print('Character 2 died. Game over.')
Of course, there are much more compact ways of writing this. This still isn't ideal but is a reduction in code...
c1sc=random.randint(1,6)
c2sc=random.randint(1,6)
if c1sc != c2sc:
while c1st > 0 and and c2st > 0:
c1st += strengthmodifier if c1sc > c2sc else -1 * strengthmodifier
c2st += strengthmodifier if c1sc < c2sc else -1 * strengthmodifier
c1sk += skillmodifier if c1sc > c2sc else -1 * skillmodifier
c2sk += skillmodifier if c1sc < c2sc else -1 * skillmodifier
print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
c1sk = math.max(c1sk, 0) # Clamp skill to a minimum of 0
c2sk = math.max(c2sk, 0) # ...
if c1st < c2st:
print('Character 1 died. Game over.')
else:
print('Character 2 died. Game over.')
else:
print('It\'s a draw, no changes were made.')
And finally, assuming a re-roll every round...
while c1st > 0 and and c2st > 0:
c1sc=random.randint(1,6)
c2sc=random.randint(1,6)
if c1sc != c2sc:
c1st += strengthmodifier if c1sc > c2sc else -1 * strengthmodifier
c2st += strengthmodifier if c1sc < c2sc else -1 * strengthmodifier
c1sk += skillmodifier if c1sc > c2sc else -1 * skillmodifier
c2sk += skillmodifier if c1sc < c2sc else -1 * skillmodifier
print('Character 1, your strength attribute is: '+(str(c1st))+' and your skill attribute is: '+(str(c1sk)))
print('Character 2, your strength attribute is: '+(str(c2st))+' and your skill attribute is: '+(str(c2sk)))
else:
print('It\'s a draw, no changes were made.')
c1sk = math.max(c1sk, 0) # Clamp skill to a minimum of 0
c2sk = math.max(c2sk, 0) # ...
if c1st < c2st:
print('Character 1 died. Game over.')
else:
print('Character 2 died. Game over.')