0

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

2
  • 1
    "this didnt work" - explain, HOW. Add the full error message. Commented Oct 17, 2020 at 21:42
  • @DYZ I tried explaining better in edit, but ill say here too. There was no error message, it just grayed out the first morality in the function Commented Oct 17, 2020 at 21:53

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

6 Comments

I tested by removing the + , it still works. Why would i have the +? Sorry, as you probably already noticed im new at this. Solved the function problem though, thank you
The += operator increments a variable by a certain amount. If you remove the +, it will still run, but the morality variable will only be the result from the last test as you'd just be reassigning the variable each time you take an input.
Actually, trying the last part does give an error message traceback (most recent call last): File "D:\Koder\PYTHON PRACTICE\Morality System.py", line 19, in <module> morality += moral(test) TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'
See my updated answer, it appears that I accidentally passed test into the moral function, not answer. I apologize for that. I also left a comma out of the list, so re-copy and paste the list of tests into your code and it should work as expected.
Ah, this is fixed then, Thank you very much good sir
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.