Python code help needed Watch

Lauren12311
Badges: 7
Rep:
?
#1
Report Thread starter 1 week ago
#1
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
Posted on the TSR App. Download from Apple or Google Play
0
quote
reply
Sonicalireza
Badges: 11
Rep:
?
#2
Report 1 week ago
#2
How much do you know so far about for loops and how you’re going to code this overall?
1
quote
reply
Lauren12311
Badges: 7
Rep:
?
#3
Report Thread starter 1 week ago
#3
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”
Posted on the TSR App. Download from Apple or Google Play
0
quote
reply
Sonicalireza
Badges: 11
Rep:
?
#4
Report 1 week ago
#4
(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)
Last edited by Sonicalireza; 1 week ago
0
quote
reply
Lauren12311
Badges: 7
Rep:
?
#5
Report Thread starter 1 week ago
#5
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
Posted on the TSR App. Download from Apple or Google Play
0
quote
reply
smithb
Badges: 3
Rep:
?
#6
Report 1 week ago
#6
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

print ([str(x).zfill(4) for x in range(10000)])
Last edited by smithb; 1 week ago
2
quote
reply
Lauren12311
Badges: 7
Rep:
?
#7
Report Thread starter 1 week ago
#7
Thank you smithb after solutions = I got a invalid syntax is there a way to fix this
Posted on the TSR App. Download from Apple or Google Play
0
quote
reply
smithb
Badges: 3
Rep:
?
#8
Report 1 week ago
#8
(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.
0
quote
reply
Lauren12311
Badges: 7
Rep:
?
#9
Report Thread starter 1 week ago
#9
Replaced dots with tabs but the solutions still has an invalid syntax I’m on python 3.7 if that makes a difference smithb
Posted on the TSR App. Download from Apple or Google Play
Last edited by Lauren12311; 1 week ago
0
quote
reply
smithb
Badges: 3
Rep:
?
#10
Report 1 week ago
#10
(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?
0
quote
reply
Lauren12311
Badges: 7
Rep:
?
#11
Report Thread starter 1 week ago
#11
(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
Posted on the TSR App. Download from Apple or Google Play
0
quote
reply
smithb
Badges: 3
Rep:
?
#12
Report 1 week ago
#12
(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
Make sure you understand the code yourself though - shoot me with any questions whenever
0
quote
reply
Lauren12311
Badges: 7
Rep:
?
#13
Report Thread starter 1 week ago
#13
(Original post by smithb)
Happy to help
Make sure you understand the code yourself though - shoot me with any questions whenever
For definite gonna try add in some functions to it now watch this fail 😂
Posted on the TSR App. Download from Apple or Google Play
0
quote
reply
Lauren12311
Badges: 7
Rep:
?
#14
Report Thread starter 1 week ago
#14
(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
Posted on the TSR App. Download from Apple or Google Play
Last edited by Lauren12311; 1 week ago
0
quote
reply
shivampaw
Badges: 10
Rep:
?
#15
Report 1 week ago
#15
(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.

Code:
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
1
quote
reply
Lauren12311
Badges: 7
Rep:
?
#16
Report Thread starter 1 week ago
#16
(Original post by shivampaw)
Use an if statement before the append to check if i is a square number.

Code:
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
Posted on the TSR App. Download from Apple or Google Play
0
quote
reply
Acsel
  • Community Assistant
Badges: 20
Rep:
?
#17
Report 1 week ago
#17
(Original post by Sonicalireza)
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,
Minor pedantic point here. The scope of the question implies they don't just want 0, 1, 2, etc. They want 4 digit combinations. So 0000, 0001, 0002, etc. The first 1000 printed numbers (0-999) would be invalid safe combinations as they are not long enough.

It is therefore necessary to pad the first 1000 numbers with leading zeroes. A simple loop from 0 to 9999 would print the numbers, but not all of those can be considered safe combinations. This is one of those examples where the practical side of things (printing numbers 0 to 9999) does not line up with the requirements (printing all possible safe combinations).
0
quote
reply
smithb
Badges: 3
Rep:
?
#18
Report 1 week ago
#18
(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)
0
quote
reply
Lauren12311
Badges: 7
Rep:
?
#19
Report Thread starter 1 week ago
#19
Thank you kindly you’re a pro at this
Posted on the TSR App. Download from Apple or Google Play
1
quote
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

University open days

  • University of Lincoln
    Mini Open Day at the Brayford Campus Undergraduate
    Wed, 19 Dec '18
  • University of East Anglia
    UEA Mini Open Day Undergraduate
    Fri, 4 Jan '19
  • Bournemouth University
    Undergraduate Mini Open Day Undergraduate
    Wed, 9 Jan '19

Did you get less than your required grades and still get into university?

Yes (22)
25.29%
No - I got the required grades (54)
62.07%
No - I missed the required grades and didn't get in (11)
12.64%

Watched Threads

View All