- 2
Code Challenge: Random divisions that can only output int numbers [Pros only]
Hey there everyone. I have been wondering if there is a way to generate two random int numbers in python. For example: import random x = random.randint(1, 10) y = random.randint(1, 10) print (str(x) + "/" +str(y)) result = x / y answer = int(input()) if answer == result: print ("Correct") else: print ("Wrong") As you can see, the code generates two numbers, prints them with a / sign between them, and asks the user their result. However, some divisions are really hard to do them mentally because they have decimals. How would you change the code so it does the same thing, but all the divisions will be a integer number?
4 Answers
+ 2
Generate two random integers and instead of dividing them multiply them. Then take the new number and one of the first two for the question.
E.g. if you generate 4 and 5, multiply them to get 20. Then set the problem as either 20/4 or 20/5.
+ 1
Good answer, but the numbers are generated between 1 and 10 for a reason. I need to generate an integer result with those numbers.
+ 1
In that case generate the first integer x between 1 and 10 and the second between 1 and 10/x.
+ 1
Now, how could I introduce it to this code? @â©âźâ
âźâ©
import pygame
import random
pygame.time.set_timer(pygame.USEREVENT, 1000)
clock = pygame.time.Clock()
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
font = pygame.font.SysFont('comicsans', 50)
base_font = pygame.font.Font(None, 32)
user_text = ''
color_active = pygame.Color('lightskyblue3')
running = True
points = 0
t = 60
def start_the_game():
x = random.randint(1, 10)
y = random.randint(0, 10)
is_correct = False
return x, y
def display_the_game(x, y):
# Variables
z = x + y
surface.fill((70, 255, 70))
text = font.render(str(x) + " / " + str(y), True, (255, 255, 255))
text_surface = base_font.render(user_text, True, (255, 255, 255))
surface.blit(text, (260, 120))
input_box.draw(surface)
punts = font.render("PuntuaciĂł: " + str(points),True, (255,255,255))
surface.blit(punts, (350,30))
titoldivisio= font.render("DivisiĂł (1)", True, (0, 0, 0))
surface.blit(titoldivisio, (10, 20))
temps = font.render("Temps: " + str(t), True, (255, 255, 51))
surface.blit(temps, (0, 360))
x, y = start_the_game()
input_box = InputBox(190, 250, 200, 32)
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.USEREVENT:
t -= 1
temps = font.render("Temps:" + str(t), True, (255, 255, 51))
surface.blit(temps, (0, 360))
pygame.display.flip()
if t == 0:
pygame.QUIT
else:
if not (x % y):
break
result = input_box.handle_event(event)
if result != None:
if int(result) == int(x) / int(y):
points = points + 5
t = t + 5
mixer.music.load('StarPost.wav')
mixer.music.play(1)