0

This is a simple newbie question. For each "i" iteration shouldn't we get a new "a" & "b" random variable. Why isn't a and b not being re-randomized with each loop iteration? I just don't see my error.

import random

for i in range(10):
    a = random.randint(1,12)
    b = random.randint(1,12)
    question = "What is "+str(a)+" x "+str(b)+"? "
    answer = input(question)
    if answer == a*b:
        print("Well done!")
    else:
        print("No.")

output
➜ python multiply.py
What is 5 x 12? 60
Well done!
What is 5 x 12? 60
Well done!
What is 5 x 12? 60
Well done!
What is 5 x 12? 60
Well done!

  • 5
    Tell us more about your environment. When I run it on my machine I get different numbers. – Andrew-Harelson Oct 07 '21 at 04:29
  • It's highly unlikely that you get the sequence `5, 12, 5, 12, 5, 12, 5, 12` from a program that should produce 20 random numbers and that you get the output "Well done!". `answer == a*b` will always be false because you compare a string with a number (the comparison could work on the outdated Python 2, but your usage of `print` indicates Python 3). I'm pretty sure that you run a different code than the code you've shown here. – Matthias Oct 07 '21 at 05:55
  • I'm new to all this and I'm just running code and showing my confusion. I suspect this has something to do with my environment as you say. I'm not trying to trick you. I wouldn't know how. I'm run this in a shell in a window of the Atom application. I use a mac with ➜ python --version Python 2.7.16 ➜ python3 --version Python 3.9.6 For my original post, I used python and when I use python 3, every answer including correct ones like 2*2 = 4 are considered false. I'm not sure what else to say about my environment. – Will Simpson Oct 07 '21 at 12:12
  • Something you guys said got me thinking that maybe my problem was Atom, and I tried using iTerm, and it worked as @Matthias said Python2 ok and Python3 produced random questions, but all answers were considered false. So this seems a problem with my setup of Atom. Back to the drawing board. Maybe I'll switch to Sublime Test as an IDE. – Will Simpson Oct 07 '21 at 12:23
  • @Matthias, how might I change the test of the answer so it correctly confirms the answer using Python 3 now that it is working in another terminal application? Why might this run in one terminal app and not another using no special virtual envirionments? – Will Simpson Oct 07 '21 at 12:26
  • 1
    You need to convert the string you input to a number (integer): `answer = int(input(question))` – Matthias Oct 07 '21 at 14:29
  • Additional: Stop using Python 2. It's dead since the beginning of 2020 and that was announced over 10 years ago. If you really, really have to use Python 2 then at least never use `input` but `raw_input` instead. `input` in Python 2 is dangerous. – Matthias Oct 07 '21 at 14:32
  • 1) To make sure you're getting python 3 in Atom on a Mac, the very first line of your script must be `#!/usr/bin/env python3`, with no spaces except between "v" and "p". MacOS still ships with python 2 as the default implementation under the name `python`, so that's what Atom uses by default as well when you tell it to run a python program. The "[shebang](https://en.wikipedia.org/wiki/Shebang_(Unix))" line overrides that. 2) If you've made any edits in program, make sure you do a save before trying to run the script from within Atom. – pjs Oct 07 '21 at 19:02

0 Answers0