0
print("welcome to the maths game")
import random
score = 0
print("Question One")
numberOne=random.randint(1,12)#These two randomly generate 2 numbers for the question
numberTwo=random.randint(1,12)      
question = str(numberOne)+ " x " + str(numberTwo) + " = " 
answer = int(input(question))                             

if answer == numberOne*numberTwo:
    print("Well Done It's Correct, Next Question")
    score + 1 #puts the score plus one
else:
    print("Better Luck Next Time, Next Question")
print("Question Two")
numberOne=random.randint(1,12)    
numberTwo=random.randint(1,12)
question = str(numberOne)+ " x " + str(numberTwo) + " = " 
answer = int(input(question))                             

if answer == numberOne*numberTwo:
    print("Well Done It's Correct, Next Question")
    score = score + 1
else:
    print("Better Luck Next Time, Next Question")
print("Question Three")
numberOne=random.randint(1,12)    
numberTwo=random.randint(1,12)
question = str(numberOne)+ " x " + str(numberTwo) + " = " 
answer = int(input(question))                             

if answer == numberOne*numberTwo:
    print("Well Done It's Correct, Next Question")
    score = score + 1
else:
    print("Better Luck Next Time, Next Question")
print("Question Four")
numberOne=random.randint(1,12)    
numberTwo=random.randint(1,12)
question = str(numberOne)+ " x " + str(numberTwo) + " = " 
answer = int(input(question))                             

if answer == numberOne*numberTwo:
    print("Well Done It's Correct, Next Question")
    score = score + 1
else:
    print("Better Luck Next Time, Next Question")
print("Question Five")
numberOne=random.randint(1,12)    
numberTwo=random.randint(1,12)
question = str(numberOne)+ " x " + str(numberTwo) + " = " 
answer = int(input(question))                             

if answer == numberOne*numberTwo:
    print("Well Done It's Correct, Thats The End")
    score = score + 1
else:
    print("Better Luck Next Time, Thats The End")

I need to shorten this code by putting this into a loop. However, when I try I can't keep the question one, two, three, four, five in each paragraph. It is to randomly generate 2 numbers 5 times for a little times table test (a Python assignment at my school).

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • Possible duplicate of [Making a quiz with shuffled questions](http://stackoverflow.com/questions/40400192/making-a-quiz-with-shuffled-questions) – Ari Gold Jan 08 '17 at 12:19
  • @AriGold I don't see how that could be considered a duplicate. This question was specific in getting the question number to be printed correctly inside a `for` loop. – roganjosh Jan 08 '17 at 12:21

3 Answers3

1

The simplest way would probably be to create a list of the words and index that list on each iteration. Obviously this becomes a problem when you want to be able to scale to any number, in which case it seems that you could use one of the libraries discussed here. To display your word, you can use the format method.

import random

number_words = ['one', 'two', 'three', 'four', 'five']
score = 0

for x in range(5):
    print("Question {}".format(number_words[x]))
    numberOne=random.randint(1,12)    
    numberTwo=random.randint(1,12)
    question = "{} x {} = ".format(numberOne, numberTwo)
    answer = int(input(question))       

    if answer == numberOne*numberTwo:
        if x != 4:
            print("Well Done It's Correct, Next Question")
        else:
            print("Well Done It's Correct, Thats The End")
        score = score + 1
    else:
        if x != 4:
            print("Better Luck Next Time, Next Question")
        else:
            print("Better Luck Next Time, Thats The End")

print("This is not in the loop")
# Write non-looping code here
Community
  • 1
  • 1
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • how do i put the loop just to this bit of code, the code below it(which i didn't show) is included in the loop and i dont want it to i will add the code in a separate comment if u want me to –  Jan 08 '17 at 13:02
  • @JarrethGriffith I have made two edits. Firstly, on the last iteration of the loop, you get a message saying "That's the end". The loop only works for code that is indented inside of that loop. So for example, you can see that I put `print("This is not in the loop")` without the indent - this will only print once, and it will only print after the loop has completed 5 times. – roganjosh Jan 08 '17 at 13:10
  • that is where i put my other code but it was being used each time, i will retry Edit-- it has worked i didnt copy my code properly sorry and than you –  Jan 08 '17 at 13:13
  • @JarrethGriffith that is not possible, as long as the code starts at the same position as the `f` in `for` then it is not in the scope of the `for` loop and will not repeat. You need to check your indentation carefully. – roganjosh Jan 08 '17 at 13:15
  • yeah i got that, its because im new to python –  Jan 08 '17 at 13:17
0

Try that one:

import random

print("welcome to the maths game")
score = 0

titles = map(lambda s: "Question %s" % s, ['One', 'Two', 'Three', 'Four', 'Five'])


def gen_randoms():
    return random.randint(1, 12), random.randint(1, 12)


for t in titles:
    print(t)
    num_one, num_two = gen_randoms()
    question = str(num_one) + " x " + str(num_two) + " = "
    answer = int(input(question))
    if answer == num_one * num_two:
        print("Well Done It's Correct, Next Question")
        score += 1  # puts the score plus one
    else:
        print("Better Luck Next Time, Next Question")

print("Your resulting score is %s" % score)
René Jahn
  • 1,155
  • 1
  • 10
  • 27
  • thank you that worked, as i am relativity new to python i don't understand some of the commands you used could you tell me what "def" ,"map","lamba", and the"%s" are for –  Jan 08 '17 at 13:10
  • def ist just the keyword for defining a new function which can be called elsewhere in the code. lambda is an anonymous function (you dont have to use def, which helps if you just want to define a function for using it once), map can use the function (first param) and call it on every element of the list (second param), "%s" is just for string formatting, – René Jahn Jan 08 '17 at 14:01
  • thank you that has helped me understand it a bit more –  Jan 08 '17 at 16:11
0
import random
def int_to_en(num):
    # http://stackoverflow.com/questions/8982163/how-do-i-tell-python-to-convert-integers-into-words
    # @SiLent SoNG
    d = { 0 : 'zero', 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five',
          6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten',
          11 : 'eleven', 12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen',
          15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen', 18 : 'eighteen',
          19 : 'nineteen', 20 : 'twenty',
          30 : 'thirty', 40 : 'forty', 50 : 'fifty', 60 : 'sixty',
          70 : 'seventy', 80 : 'eighty', 90 : 'ninety' }
    k = 1000
    m = k * 1000
    b = m * 1000
    t = b * 1000
    assert(0 <= num)
    if (num < 20):
        return d[num]
    if (num < 100):
        if num % 10 == 0: return d[num]
        else: return d[num // 10 * 10] + '-' + d[num % 10]
    if (num < k):
        if num % 100 == 0: return d[num // 100] + ' hundred'
        else: return d[num // 100] + ' hundred and ' + int_to_en(num % 100)
    if (num < m):
        if num % k == 0: return int_to_en(num // k) + ' thousand'
        else: return int_to_en(num // k) + ' thousand, ' + int_to_en(num % k)
    if (num < b):
        if (num % m) == 0: return int_to_en(num // m) + ' million'
        else: return int_to_en(num // m) + ' million, ' + int_to_en(num % m)
    if (num < t):
        if (num % b) == 0: return int_to_en(num // b) + ' billion'
        else: return int_to_en(num // b) + ' billion, ' + int_to_en(num % b)
    if (num % t == 0): return int_to_en(num // t) + ' trillion'
    else: return int_to_en(num // t) + ' trillion, ' + int_to_en(num % t)
    raise AssertionError('num is too large: %s' % str(num))

def get_question(num, *args):
  # param: num, convert int to word (instead list = ['one', 'two', 'three', 'four', 'five'.......])
  # your game should be scalable for N number of questions

  q_header = 'Question {0}:'.format(int_to_en(num)) # convert and add it to string, use {n} with index, faster than without
  r_N_a, r_N_b = [random.randint(1,12) for __ in range(2)] # to random num
  q_str = '{0} x {1} = '.format(r_N_a, r_N_b) #  creating question string  
  return (q_header, r_N_a*r_N_b, q_str) # return, you could now call get_question method from outside, if you need a question block in your other modules 

def run_game(num, score = 0):
  for n in range(num):
    q_header, expectation, q_str = get_question(n)
    print (q_header)
    answer = int(input(q_str))
    if not answer == expectation:
      print ('wrong')
    else:
      score += 1
      print ('next')

  return score

score = run_game(3) # 3 questions

block = [get_question(n) for n in range(50)] # 50 question/solution block
print (block)

print (block[24])    

[('Question zero:', 40, '5 x 8 = '), ('Question one:', 80, '8 x 10 = '), ('Question two:', 49, '7 x 7 = '), ('Question three:', 2, '1 x 2 = '), ('Question four:', 60, '12 x 5 = '), ('Question five:', 77, '7 x 11 = '), ('Question six:', 144, '12 x 12 = '), ('Question seven:', 77, '7 x 11 = '), ('Question eight:', 88, '8 x 11 = '), ('Question nine:', 5, '1 x 5 = '), ('Question ten:', 40, '5 x 8 = '), ('Question eleven:', 72, '12 x 6 = '), ('Question twelve:', 24, '4 x 6 = '), ('Question thirteen:', 72, '9 x 8 = '), ('Question fourteen:', 1, '1 x 1 = '), ('Question fifteen:', 2, '2 x 1 = '), ('Question sixteen:', 24, '6 x 4 = '), ('Question seventeen:', 80, '8 x 10 = '), ('Question eighteen:', 36, '4 x 9 = '), ('Question nineteen:', 99, '11 x 9 = '), ('Question twenty:', 42, '6 x 7 = '), ('Question twenty-one:', 12, '12 x 1 = '), ('Question twenty-two:', 96, '12 x 8 = '), ('Question twenty-three:', 50, '5 x 10 = '), ('Question twenty-four:', 72, '12 x 6 = '), ('Question twenty-five:', 80, '8 x 10 = '), ('Question twenty-six:', 24, '3 x 8 = '), ('Question twenty-seven:', 88, '8 x 11 = '), ('Question twenty-eight:', 108, '9 x 12 = '), ('Question twenty-nine:', 12, '3 x 4 = '), ('Question thirty:', 144, '12 x 12 = '), ('Question thirty-one:', 2, '2 x 1 = '), ('Question thirty-two:', 56, '8 x 7 = '), ('Question thirty-three:', 32, '8 x 4 = '), ('Question thirty-four:', 5, '1 x 5 = '), ('Question thirty-five:', 16, '4 x 4 = '), ('Question thirty-six:', 84, '7 x 12 = '), ('Question thirty-seven:', 44, '11 x 4 = '), ('Question thirty-eight:', 7, '7 x 1 = '), ('Question thirty-nine:', 54, '9 x 6 = '), ('Question forty:', 22, '2 x 11 = '), ('Question forty-one:', 77, '7 x 11 = '), ('Question forty-two:', 20, '5 x 4 = '), ('Question forty-three:', 9, '1 x 9 = '), ('Question forty-four:', 8, '2 x 4 = '), ('Question forty-five:', 24, '8 x 3 = '), ('Question forty-six:', 15, '5 x 3 = '), ('Question forty-seven:', 24, '8 x 3 = '), ('Question forty-eight:', 30, '6 x 5 = '), ('Question forty-nine:', 96, '12 x 8 = ')]

('Question twenty-four:', 110, '11 x 10 = ')
Ari Gold
  • 1,528
  • 11
  • 18
  • @ Steven Summers, thx, I will implement your objective critique promptly. It is a great pleasure for the coming readers and me – Ari Gold Jan 08 '17 at 14:25