So Basically I have to write a program that asks the user for the difficulty level from the user and print questions and even the limit of questions asked from the user. I am facing an issue that I am not able to print questions like these:
- What is 21 86?
- What is 92/2?
- What is 86-70?.
number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)
When I use this command I get number like 19899 198846. which I don't want. I have no idea what to do.
import random
print ("Welcome Little Brother!")
#This Educational Software will help you with your arithmetic problem and polish your skills.
score = 0
print("Please make a selection from the following:")
print("P: Practice Math.")
print("S: Show Score.")
print("Q: Quit.")
selection = input("What do you want to do:")
if selection == "S" :
print ("No score found.")
elif selection == "P":
print("What difficult level do you want:")
print("E: Easy.")
print("M: Medium.")
print("H: Hard.")
level = input("Enter Difficulty Level:")
numberofproblem = int(input("How many problems do you want :"))
max_number = numberofproblem
for question_num in range(1, max_number 1):
if level == "E" :
ops = [' ', '-']
elif level == "M" :
ops = [' ', '-', '*']
elif level == "H" :
ops = [' ', '-', '*','/']
else:
print ("Incorrect Menu Key Selected")
number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)
operation = random.choice(ops)
maths = round(eval(str(number_1) operation str(number_2)),5)
print('\nQuestion number: {}'.format(question_num))
print ("The question is",number_1,operation,number_2)
answer = float(input("What is your answer: "))
if answer == maths:
print("Correct")
score = score 1
else:
print ("Incorrect. The actual answer is",maths)
CodePudding user response:
You can make a dictionary to choose the difficulty and assign it to a function to run that difficulty quiz
def easy():
#Code
def medium():
#code
def hard():
#code
diff = {'easy':easy,'medium':medium,'hard':hard}
Difficulty = input("Enter Difficulty:")
for i in diff:
if(Difficulty == diff[i]:
diff[i]()
else:
continue
CodePudding user response:
According to the documentation of randrange()
, the values are generated in the range [a,b)
.
number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)
This means that in your case a random value between 1
and 100'000
is generated. Try to lower the second number so that any number generated is acceptable. 100
might be a good fit for your example.