The Student Room Group

Python code help needed

I have to create a program using a for loop to print out all the possible 4 digit combinations to open a safe. Could anyone help me with the code please for this
How much do you know so far about for loops and how you’re going to code this overall?
Literally started learning python about 2 weeks ago so my knowledge is literally as basic as it gets. All the question says is to write a program using a for loop to print out the four digit numbers that could be the safe combination”
Original post by Lauren12311
Literally started learning python about 2 weeks ago so my knowledge is literally as basic as it gets. All the question says is to write a program using a for loop to print out the four digit numbers that could be the safe combination”

Ok so I’m not sure how complex of a solution it wants but essentially I’m guessing all it wants is for the program to print all numbers from 0 to 9999 and a for loop would make it repeat as many times as you want it to (in this case 9999 times) so the code would just be a for loop that starts by printing 0 then 1,2,3....9998,9999 and it repeats 9999 times,
You can just call the first variable x and make it =0 then in the loop it just adds one everytime and also prints it (I’m hoping you can try to figure it out since I don’t want to give the answer right away)
(edited 5 years ago)
Thank you so much you’re a star I’ll give that a go. And then try and add the functions I worked out before in
Reply 5
Nested loops - 7 lines. The dots are to show indentation.

solutions = []
for i in range(10):
....for j in range(10):
........for k in range(10):
............for l in range(10):
................solutions.append(str(str(i) + str(j) + str(k) + str(l)))
print (solutions)

Alternative solution - str.zfill(4) means this can be done in 4 lines.

solutions = []
for i in range(10000):
....solutions.append(str(i).zfill(4))
print (solutions)

Tag me if you find any errors, would like more explanation, or find a cooler solution

edit - Previous solution can be condensed to 1 line :biggrin:

print ([str(x).zfill(4) for x in range(10000)])
(edited 5 years ago)
Thank you @smithb after solutions = I got a invalid syntax is there a way to fix this
Reply 7
Original post by Lauren12311
Thank you @smithb after solutions = I got a invalid syntax is there a way to fix this


Try fully typing out the second solution, and replacing the dots with a tab. :smile:
Replaced dots with tabs but the solutions still has an invalid syntax I’m on python 3.7 if that makes a difference @smithb
(edited 5 years ago)
Reply 9
Original post by Lauren12311
Replaced dots with tabs but the solutions still has an invalid syntax I’m on python 3.7 if that makes a difference


They are square brackets if that helps. Send me a screenshot of your code / error message?
Original post by smithb
They are square brackets if that helps. Send me a screenshot of your code / error message?


Thank you so much it worked I completely missed the brackets 🤦🏽*♀️ told you I’m a beginner don’t even know what a for loop is
Reply 11
Original post by Lauren12311
Thank you so much it worked I completely missed the brackets 🤦🏽*♀️ told you I’m a beginner don’t even know what a for loop is


Happy to help :u:
Make sure you understand the code yourself though - shoot me with any questions whenever :biggrin:
Original post by smithb
Happy to help :u:
Make sure you understand the code yourself though - shoot me with any questions whenever :biggrin:


For definite gonna try add in some functions to it now watch this fail 😂
Original post by Lauren12311
For definite gonna try add in some functions to it now watch this fail 😂


Last question sorry to be a pain. Using the code you sent is there a way to add my function so it only returns numbers that are perfect squares? @smithb
(edited 5 years ago)
Original post by Lauren12311
Last question sorry to be a pain. Using the code you sent is there a way to add my function so it only returns numbers that are perfect squares? @smithb


Use an if statement before the append to check if i is a square number.


root = math.sqrt(i)
if int(root + 0.5) ** 2 == i:
# append line here


You’ll need to import math as well at the top
Original post by shivampaw
Use an if statement before the append to check if i is a square number.


root = math.sqrt(i)
if int(root + 0.5) ** 2 == i:
# append line here


You’ll need to import math as well at the top


Thank you so much
Reply 16
Original post by Lauren12311
Last question sorry to be a pain. Using the code you sent is there a way to add my function so it only returns numbers that are perfect squares? @smithb


solutions = []

def isSquare(x):
....if (x**0.5) % 1 == 0:
........return True

for i in range(10000):
...if isSquare(i):
........solutions.append(str(i).zfill(4) )

print (solutions)
Thank you kindly you’re a pro at this

Quick Reply

Latest

Trending

Trending