简体   繁体   中英

so i started programming 1 week ago and i am a bit confused about why my program is not subtracting 1 when answer to any question is incorrect

i cant get to subtract one from score in this python program. when i run the code it shows answer from 0 to 4 but not in negative i want the answer to be negative if too many answers are wrong. Here is the code:

***print("Hello and welcome to my quiz! ")
score = 0

# this the question i want it to add 1 if answer is correct and subtract 1 if answer is incorrect  

print("question 1: what is my name? ")
ans1 = input()
if ans1 == 'mark':
    print('correct')
    score += 1
else:print('incorrect')
    score -= 1

# second question
print("question 2: what is my age? ")
ans2 = input()
if ans2 == '19':
    print('correct')
    score += 1
else:print('incorrect')
score -= 1
print("question 3: what is my fathers name? ")
ans3 = input()
# third question
if ans3 == 'john':
    print('correct')
    score += 1
else:print('incorrect')
score -= 1

**# fourth question**

print("question 4: what is my mothers name? ")
ans4 = input()
if ans4 == 'Emily':
    print('correct')
    score += 1
else:print('incorrect')
score -= 1
print ('your score is', score ) 
# answer can be in negative***

This actually shouldn't even run.

else:print('incorrect')
    score -= 1

You need to either have a one-line else statement or put all code for the else statement on the next line and properly indent it. Python is all about the whitespace.

The following should fix your issue.

else:
    print('incorrect')
    score -= 1

The increments are going to be invoked regardless of the answer. For example

# second question
print("question 2: what is my age? ")
ans2 = input()
if ans2 == '19':
    print('correct')
    score += 1
else:print('incorrect')
score -= 1

If the answer is correct, then the score will be incremented but then the score will also then be decremented, regardless. The score -=1 needs to be indented correctly, like this

# second question
print("question 2: what is my age? ")
ans2 = input()
if ans2 == '19':
    print('correct')
    score += 1
else:
    print('incorrect')
    score -= 1

If you try this across your script, the results may be more like you expect.

The program is almost fine. I am getting negative score if I answer everything uncorrectly. The last line is wrong, since you can't concatenate an integrer with a string. Also, you need to properly indent the content under the "else" statements. This would be the finished program:

print("Hello and welcome to my quiz! ")
score = 0

# this the question i want it to add 1 if answer is correct and subtract 1 if answer is incorrect

print("question 1: what is my name? ")
ans1 = input()
if ans1 == 'mark':
    print('correct')
    score += 1
else:
    print('incorrect')
    score -= 1

# second question
print("question 2: what is my age? ")
ans2 = input()
if ans2 == '19':
    print('correct')
    score += 1
else:
    print('incorrect')
    score -= 1
print("question 3: what is my fathers name? ")
ans3 = input()
# third question
if ans3 == 'john':
    print('correct')
    score += 1
else:
    print('incorrect')
    score -= 1

# fourth question**

print("question 4: what is my mothers name? ")
ans4 = input()
if ans4 == 'Emily':
    print('correct')

print('your score is ' + str(score) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM