4 Replies - 512 Views - Last Post: 03 October 2017 - 10:01 AM Rate Topic: -----

#1 ShadowWarrior17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-September 17

Need a little help with basic python programming!

Posted 15 September 2017 - 11:04 AM

I'm creating a program which is a modular maths quiz.
I'm currently trying to prevent the random numbers,that are generated, when divided, to not produce a decimal, and to keep making new numbers until the answer is a whole number(while loop).
I've been told to do this using modulus and defining a new function with my randomly generated numbers.
Here is my code:
import random
print("Welcome to Brain Training!")
print("You will be asked 10 questions.")
name=input("What is your name?")
nick=input("What is your nickname?")

difficulty=int(input("Which level of difficulty would you like to choose? (1-10)"))
while difficulty<1 or difficulty>10:
  difficulty=int(input("Which level of difficulty would you like to choose? (1-10)"))
rnum1=random.randint(1,5)

rnum2=random.randint(1,10)
num1=rnum1*difficulty
num2=rnum2*difficulty
questionchoice=random.randint(1,4)
nothing=0
questionnumber=1

questions=int(input("How many questions would you like? (Minimum 5, maximum 20)"))
while questions<5 or questions>20:
  questions=int(input("How many questions would you like? (Minimum 5, maximum 20)"))

while questionnumber < questions:
  print("Question",questionnumber,":")
  if questionchoice == 1:
    print("What is",num1,"+",num2,"?")
    ans1=int(input())
  elif questionchoice == 2:
    print("What is",num1,"-",num2,"?")
    ans2=int(input())
  elif questionchoice == 3:
    print("What is",num1,"*",num2,"?")
    ans3=int(input())
  elif questionchoice == 4:
    print("What is",num1,"/",num2,"?")
    ans4=int(input())
  answer1=num1+num2
  answer2=num1-num2
  answer3=num1*num2
  answer4=num1/num2
  if questionchoice == 1 and answer1 == ans1:
    print("You are correct! Well Done!")
    score=nothing+1
    print("Score:",score)
  elif questionchoice == 2 and answer2 == ans2:
    print("You are correct! Well Done!")
    score=nothing+1
    print("Score:",score)
  elif questionchoice == 3 and answer3 == ans3:
    print("You are correct! Well Done!")
    score=nothing+1
    print("Score:",score)
  elif questionchoice == 4 and answer4 == ans4:
    print("You are correct! Well Done!")
    score=nothing+1
    print("Score:",score)
  else:
    print("You are incorrect.")
  questionchoice=questionchoice+1
  questionnumber=questionnumber+1



I've also been instructed to tell the user that the numbers did not cleanly divide, and to generate new numbers until they do.

2 more things:
How would I let the user play again, I understand the whole program will have to be in a while loop then, but what would I write for it?
How would I keep the high score as a variable while the user plays again?

Appreciate the help in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Need a little help with basic python programming!

#2 ShadowWarrior17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-September 17

Re: Need a little help with basic python programming!

Posted 15 September 2017 - 12:25 PM

I have figured out how to find out whether the numbers can't cleanly divide, and how to present that to the user. Just can't figure out how to re-generate the numbers while keeping the divide operator.
Was This Post Helpful? 0
  • +
  • -

#3 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,048
  • Joined: 27-December 13

Re: Need a little help with basic python programming!

Posted 15 September 2017 - 01:08 PM

As for the the game loop, this tutorial (second part) may be helpful: http://www.dreaminco...and-game-loops/
Probably this will also cover you last question...
Was This Post Helpful? 0
  • +
  • -

#4 albert003  Icon User is online

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 367
  • Joined: 15-December 14

Re: Need a little help with basic python programming!

Posted 03 October 2017 - 12:16 AM

I would suggest a game loop and use functions.
Was This Post Helpful? 0
  • +
  • -

#5 woooee  Icon User is offline

  • D.I.C Head

Reputation: 45
  • View blog
  • Posts: 168
  • Joined: 21-November 12

Re: Need a little help with basic python programming!

Posted 03 October 2017 - 10:01 AM

Use divmod for the equal division problem
numlist=[[3,3], (3,2), (6, 4)]
for num1, num2 in numlist:
    whole, remain=divmod(num1, num2)
    lit="Evenly divides"
    if remain:
       lit= "Does Not evenly divide"
    print(num1, "/", num2, "results in", whole, remain, lit) 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1