user7380601
user7380601

Reputation:

Does anyone know how i can put this into a loop?

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).

Upvotes: 0

Views: 117

Answers (3)

Ari Gold
Ari Gold

Reputation: 1548

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 = ')

Upvotes: 0

roganjosh
roganjosh

Reputation: 13175

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

Upvotes: 1

Ren&#233; Jahn
Ren&#233; Jahn

Reputation: 1213

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)

Upvotes: 0

Related Questions