Python arithmetic quiz task 1

2024/11/9 9:42:16

I have no idea why this code is not working, as you can see I'm trying to ask the user 10 questions display their score at the end. Everything works except that the score will always appear as a 0 or 1 at the end, even if more than 1 question was answered right. Here is my code:

import random
studentname=input("what is your name?:")
def question():global operationglobal number1global number2global studentanswerglobal scoreoperation=random.choice(["*","-","+"])score=0trueanswer=0number1=random.randrange(1,10)number2=random.randrange(1,10)print("what is", number1,operation,number2,"?:")studentanswer=int(input("insert answer:"))def checking():global scoreif operation == "*":trueanswer = number1*number2if studentanswer == trueanswer:print("correct")score=score+1else:print("incorrect")score=scoreelif operation == "-":trueanswer = number1-number2if studentanswer == trueanswer:print("correct")score=score+1else:print("incorrect")score=scoreelif operation == "+":trueanswer = number1+number2if studentanswer == trueanswer:print("correct")score = score+1else:print("incorrect")score=scoredef main():for i in range (10):question()checking()print("your score is", score)main()
Answer

Each call to question() resets score to be 0. Remove the score=0 line from that function, and instead initialize it in main:

def main():global score;score = 0;for i in range (10):question()checking()print("your score is", score)
https://en.xdnf.cn/q/120523.html

Related Q&A

Using Python to split long string, by given ‘separators’ [duplicate]

This question already has answers here:Split Strings into words with multiple word boundary delimiters(31 answers)Closed 9 years ago.Environment: Win 7; Python 2.76I want to split a long string into pi…

How do I return the number of unique digits in a positive integer

Example: unique_dig(123456) All unique 6Im trying to write code to have a function return how many unique numbers there are in a positive integer.count = 0for i in unique_digits:if count.has_key(i):cou…

Python check json file with variables

I have a json file which has 18 substrings like this: https://i.sstatic.net/aVWuw.png https://i.sstatic.net/RLlRX.pngBut I have more json files who have different number of these substrings. So I did t…

The Sum of Consecutive Numbers in Python

What I have to do is get the user input and add consecutive numbers starting with one using a loop until the sum equals or exceeds the input. Its an exercise, so Im trying to do this without using the …

how to write to a text file using python ?

I am trying to output a full for iteration. The output should be in a text file. How should I code for that ? The output should look like :Iteration 1 values --------> val1 < tab > val2 < …

Python: Sorting dictionary by key

I am trying to sort a dictionary by key.If I do the following, then the dictionary is sorted like this1, 20 10, 5 11, 3 2, 30 20, 2Instead, I wanted to sort it like the following:1, 20 2, 30 10, 5 11, …

Please see my problem, believe me it is easy to solve

i tried to implement async and await inside spawn child process. But it didnt worked. Please see this Expected output************* http://www.stevecostellolaw.com/************* http://www.stevecostello…

How to make discord bot ping users using discord.py [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 edit hostname file using fabric

I have change my hosts file,so how to change hostname.my system is ubuntu. eg my hosts file:192.168.0.100 host1.mydomain.com 192.168.0.101 host2.mydomain.comI wanna the hostname file under /etc/hostnam…

functions and indentation in python [duplicate]

This question already has answers here:How do I get ("return") a result (output) from a function? How can I use the result later?(4 answers)Closed 3 days ago.Im taking a tutorial in udemy t…