Reputation: 13
Though i sorry title is pretty bad its a problem im struggling with at the moment
My current code is pretty ugly
morality = 0
test = input("You see someone by a cliff, do you:\n1.Warn them about getting too close\n2.Push them off\n")
if test == "1":
morality = morality + 1
elif test == "2":
morality = morality - 1
test = input("A child drops its icecream, do you:\n1.Console the child\n2.Laugh and mock the child\n")
if test == "1":
morality = morality + 1
elif test == "2":
morality = morality - 1
test = input("You are given immunity and a gun, do you:\n1.Kill someone\n2.Not kill someone\n")
if test == "1":
morality = morality + 1
elif test == "2":
morality = morality - 1
test = input("You are given the cure to aids, do you:\n1.Cure aids\n2.Destroy the cure\n")
if test == "1":
morality = morality + 1
elif test == "2":
morality = morality - 1
if morality == -4:
print("You absolute evil man")
elif morality == -1 or morality == -2 or morality == -3:
print("you kinda evil man")
elif morality == 1 or morality == 2 or morality == 3:
print("You kinda nice")
elif morality == 4:
print("pretty nice person aint ya")
Its supposed to be a draft of a morality system, but its big and messy
i tried creating a function called moral
def moral():
if test == "1":
morality = morality + 1
elif test == "2":
morality = morality - 1
but for a reason i dont know, this didnt work. I use Pycharm and pycharm just kind of grayed out the first morality. No error message or anything, just grayed out
Basically, i want to beautify my code, but i have no idea how
Upvotes: 0
Views: 47
Reputation: 3155
You are off to a great start: functions are extremely important in shortening code and are fundamental to the success of most programs.
The moral()
function has two main issues, which go hand-in-hand. Functions have a local scope, so they are not able to access variables outside of their scope unless the variable is declared global
. Beware, the global
keyword is discouraged as it can make your code difficult to understand and increasingly messy as your project expands as it allows functions to have possible unwanted side effects. In your code, you don't initialize morality and test (possibly why it is grayed out), but you try to access and modify it.
def moral(test):
if test == "1":
return 1
elif test == "2":
return -1
Above, moral()
takes one parameter, "test" which you can pass in the resut of the test. Then, you can easily increment a variable with the returned value from that function.
For example:
morality = 0
test = input("You are given the cure to aids, do you:\n1.Cure aids\n2.Destroy the cure\n")
morality += moral(test)
In terms of prettifying your code, since you have a repeating action, you can simply create a list of all of the prompts and then prompt the user's input via iteration.
tests = [
"You see someone by a cliff, do you:\n1.Warn them about getting too close\n2.Push them off\n",
"A child drops its icecream, do you:\n1.Console the child\n2.Laugh and mock the child\n",
"You are given immunity and a gun, do you:\n1.Kill someone\n2.Not kill someone\n",
"You are given the cure to aids, do you:\n1.Cure aids\n2.Destroy the cure\n"
]
morality = 0
for test in tests:
answer = input(test)
morality += moral(answer)
Upvotes: 1