Lauren12311
Badges: 14
Rep:
?
#1
Report Thread starter 1 year 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
0
reply
Sonicalireza
Badges: 11
Rep:
?
#2
Report 1 year ago
#2
How much do you know so far about for loops and how you’re going to code this overall?
1
reply
Lauren12311
Badges: 14
Rep:
?
#3
Report Thread starter 1 year 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”
0
reply
Sonicalireza
Badges: 11
Rep:
?
#4
Report 1 year 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 year ago
0
reply
Lauren12311
Badges: 14
Rep:
?
#5
Report Thread starter 1 year 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
0
reply
smithb
Badges: 3
Rep:
?
#6
Report 1 year 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 year ago
2
reply
Lauren12311
Badges: 14
Rep:
?
#7
Report Thread starter 1 year ago
#7
Thank you smithb after solutions = I got a invalid syntax is there a way to fix this
0
reply
smithb
Badges: 3
Rep:
?
#8
Report 1 year 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
reply
Lauren12311
Badges: 14
Rep:
?
#9
Report Thread starter 1 year 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
Last edited by Lauren12311; 1 year ago
0
reply
smithb
Badges: 3
Rep:
?
#10
Report 1 year 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
reply
Lauren12311
Badges: 14
Rep:
?
#11
Report Thread starter 1 year 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
0
reply
smithb
Badges: 3
Rep:
?
#12
Report 1 year 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
reply
Lauren12311
Badges: 14
Rep:
?
#13
Report Thread starter 1 year 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 😂
0
reply
Lauren12311
Badges: 14
Rep:
?
#14
Report Thread starter 1 year 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
Last edited by Lauren12311; 1 year ago
0
reply
shivampaw
Badges: 12
Rep:
?
#15
Report 1 year 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
reply
Lauren12311
Badges: 14
Rep:
?
#16
Report Thread starter 1 year 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
0
reply
smithb
Badges: 3
Rep:
?
#17
Report 1 year ago
#17
(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
reply
Lauren12311
Badges: 14
Rep:
?
#18
Report Thread starter 1 year ago
#18
Thank you kindly you’re a pro at this
1
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
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 Hertfordshire
    All Subjects Undergraduate
    Sat, 22 Feb '20
  • Ravensbourne University London
    Undergraduate Open Day Undergraduate
    Sat, 22 Feb '20
  • Sheffield Hallam University
    Get into Teaching in South Yorkshire Undergraduate
    Wed, 26 Feb '20

People at uni: do initiations (like heavy drinking) put you off joining sports societies?

Yes (409)
67.6%
No (196)
32.4%

Watched Threads

View All