#🔒 Confused?
349 messages · Page 1 of 1 (latest)
@trim elm
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
You're on the right track, but you're thinking about it backwards
we want to repeat asking them when it ISN'T a positive number
The loop asks over and over until they enter something positive
wait wait
while number > 0: wouldnt this repeat if the user inputs something less than 0?
yeah
(no)
so "while number is less than 0, ask the user for a number"
no?
that code which they have in their message would not repeat if the user inputs a number less than 0
this should be reversed. you'd only want to continue the loop, as long as the number typed is wrong (i.e less than zero)
so it'll be while number < 0: ...
ah okay i see now
so now that covers the check if its positive or negative
sorry you're right, I misread
we often want to think of while loops flipped from how we would do our if
once the loop ends, we know the int will be positive
we want to perform the list of multiplication opeartions
we don't need to do an additional check
so we just print outside of the loop?
yeah print outside of the loop bcs it keeps looping until its positive
you'll want another loop now to handle printing the multiplication table
but what would the condition be? we dont need to check if its positive now we need to have it so the list of operations doenst exceed the number
There's no condition necessary
have you learned for loops yet?
oh sorry just read your original message
ok then yes, you'll need some new variables here
i could set i = 0 then on the next line while i < number and increment i each time
so number is going to act as the max value
yes exactly 🙂
yes
but you'll need a nested loop
so you'll want another variable to keep track of the count of the inner loop as well
j = 0 would work nicely
would i put it inside the while loop or outside below the i variable?
yes perfect
no : should be at the end of the line though
for j = 0
well how high should the inner loop count?
till the number?
wait lets go back a step how comes we would need a nested loop for this? I'd like to understand this too before moving on
bcs we've dealt with the part to ensure the list of multiplcation opeartsions doesnt exceed the number ( all i havent added in yet is the increment part)
is the nested loop going to be needed for the actual printing out?
the nested loop is needed to get every possible combination
if your number is 3, you need every combination of 1, 2, 3 x 1, 2, 3
oh okay so j needs to keep on going until number so while j < number:
thats the condition done how do we now figure out each combination for each number
well within the loops, you're going to have to increment j and i
yup otherwise they'll go on infinitely
also just to think about the final output, you probably don't need 0, and you do want number
so i and j can start at 1
and your condition should be <= instead of <
if would also include 0 for the numbers
cool yeah we dont need 0
do we need to print out the combinations now using a f string?
Yes exactly 🙂
oops wrong channel
print(f"{j} x {}") what would i put in the second curly bracket ?
what other variables do you have available?
you only need 1 print
oh
it happens inside the inner loop
did it 
now let's see why most people prefer for loops here 🙂
!e
for i in range(1, 4):
for j in range(1, 4):
result = i * j
print(f'{i} x {j} = {result}')
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | 1 x 1 = 1
002 | 1 x 2 = 2
003 | 1 x 3 = 3
004 | 2 x 1 = 2
005 | 2 x 2 = 4
006 | 2 x 3 = 6
007 | 3 x 1 = 3
008 | 3 x 2 = 6
009 | 3 x 3 = 9
can you share your code?
sure
this looks way more efficient
for loops make simple counting much simpler
for loops and range make this alot more convenient
tbh i cant even think of how to do this with nested while loops
but im pretty dumb
number = int(input("Please type in a number: "))
while number < 0:
number = int(input("Please type in a number: "))
i = 1
while i <= number:
j = 1
while j <= number:
print(f"{i} x {j} = {j*i}")
j+= 1
i+= 1
idk how to format the code properly ðŸ˜
yeah this is exactly it
but you get the idea
so you don't have to repeat yourself, it's common to assign a "dummy" value before the first while loop
with the correct indentations
number = -1
while number < 0:
number = int(input("Please type in a number: "))
i = 1
while i <= number:
j = 1
while j <= number:
print(f"{i} x {j} = {j*i}")
j+= 1
i+= 1
oh that works yeah
didnt even think of it like that
anyways another exercise done and dusted (im about to be stuck on another one
)
that's good though! Seems like you got through this ok with a bit of thought
If you haven't been, I really recommend having your own editor where you can test code for these exercises
did this actually want a nested loop btw? it dosent seem like its asking for one
it makes testing really quick, and you can see your results right away
you can test the code on their online interpreter too
How would you do it without?
yh i was just taught the idea of nested loops so i'd assume so
hes doing the mooc.fi course
my smooth brain just did this
unit_1 = 1
unit_2 = 1
number = int(input("Please input a positive number: "))
if number >= 0:
print("Enter a number above 0")
while unit_1 <= number:
print(f'{unit_1} x {unit_2} = {unit_1 * unit_2}')
unit_2 += 1
if unit_2 > number:
unit_2 = 1
unit_1 += 1
does it run the tests of the exercise though?
it does
yeah it does
yeah, you should use an editor where you're just free to see results without the testing
like vscode?
once he gets to the later parts the course uses tmc inside of vscode to do things
sure, or pycharm/idle/sublime/thonny
this is basically just a while loop with more steps
yeah
it just makes more sense in my head like this if i cant use range or for loops though
a for loop is actually a while loop in disguise
so it helps to think of them that way
yeah
finished it ? how was it
i finsihed it yeah
its been pretty good so far
its pretty good
but since it was in finish originally sometimes the questions read kinda weirdly
were you already proficient in another language or was python your first?
nice what have you moved onto do since completing the mooc?
kinda stuck in a rut atm tbh ive never been good with self learning lol
same lol but completing the mooc means youve pretty much learnt a decent amount of python
very slowly going through their dap course atm but they removed it recently
What did you learn python for?
i just kinda learned it to get into programming tbh
i didnt have any goal in mind at the time
cyber security and ai seem interesting but i dont feel like i have enough knowledge to branch into those atm
for ai/ml i think kaggle has good intro courses
how long did the python mooc take you and how much time did u spend on it per day?
it took me a while since i eventually only started doing one or two exercises a day
took me a few months to do the advanced course
could have done it a lot faster
but i was maybe 1-3 hours a day?
some of the later exercises took me a while
when my motivation existed atleast
the basic course shouldnt take that long tbh
did u do any random projects outside of the mooc?
i did do some, but i could never really think of what to make
im on part 3 so far apart from me procrastinating so far hasnt been that long
!projects
The Kindling projects page contains a list of projects and ideas programmers can tackle to build their skills and knowledge.
the only real unique project i made was hangman with llm integration for hints
sounds cool maybe ask chatgpt for some ideas and choose the ones that interest you
im already stumped ðŸ˜
was it normal to get stuck at an exercise beginning like this
yeah
everything is new so its expected
the lectures on the course do help abit
they are pretty dry though
calm
Still can't use a for loop?
nope
Do you know how to split a string up like that into its words?
as in string slicing?
nope
all ive learnt so far is
input, variables, arithmetic operations, conditional statements, while loops, nested loops, string slicing index stuff
would you know how to print every letter of the string 1 at a time?
they should be expecting you to use string slicing for this
I don't see where slicing would help here
or indexing
yeah, definitely indexing
i used slicing in my really dumb solution
while True:
print(sentence[0:])
what happens when you run that?
prints infinitely oops
Do you understand what indexing is?
until you reach the end of the string
So you've already seen how to make a number count up using a while loop
but how do you know when to stop the loop?
"can't use for loops" as in OP doesn't understand for loops, or isn't allowed to use for loops?
until i is == len of the word
they're only using what they've been taught so far in this course
ah okay
nearly
len - 1
if a string has 10 letters, the index is 0-9
oh yeah bcs length is always one more
so
ive written this out so far
perfect
ok, now let's think about what it actually wants you to print
the first letter of each word
yup
which is basically "the first character after you encounter a space"
(and also the very first letter of the string)
we can do this a few different ways
so we want some sort of if statement to check if theres a space?
yes exactly
yes
There's a technique called "boolean flags"
basically, we have a boolean that keeps track if we've recently seen a space
I really like to think about them like light switches
like on = true
off = false ?
exactly
so here's the setup
If we see a space, turn on the lightswitch
If the lightswitch is on, print the letter and turn off the lightswitch
The order we do this in is important though. I actually described it in reverse
First we check if the lightswitch is on. If it is, print the letter and turn off the light switch
If the current letter is a space, turn on the lightswitch
so would I need variables for the "switches"
just 1 variable, yeah
and would it initialise it to true or false
false right since it starts at the beginning
That depends if you want to print the first letter or not 😉
If you always want to print the first letter, then it should start on
because we print letters when the switch is on
but if the switch is on from the start wouldnt it skip the first letter until it finds the space
and prints the letter after it
we flip on the switch to indicate that the next letter should print
ohh
but if we start with it on, the first letter is the next letter
makes sense
so that would be (in words) if the index i'm on is a space, then set the variable shouldprint to true, then if shouldprint is true, print out the letter by referencing the index?
if switch == True:
print(sentence[i])
switch = False
perfect
a neat thing about booleans is we don't have to actually write the == True
we can just do if switch
i was gonna interrupt but nevermind - my apologies
oh since its already true
Yes, but remember it's super important the order that we do this
no worries
because we don't want to turn on the switch and then check if it's on
it should check the next letter
so now we can successfully print the first letter
now we'd need a condition for when the switch is false?
We don't need a condition for when the switch is False
if it's false, we don't do anything
the other condition is "is this letter a space"
exactly 🙂
no nesting
it needs to be inside the while loop
but not inside the other condition
let's see it
sentence = str(input("Please type in a sentence: "))
switch = True
i = 0
while i <= len(sentence) - 1:
if switch:
print(sentence[i])
switch = False
elif sentence[i] == " ":
switch = True
i+= 1
it doesn't need to be elif, but nice!
it will start to matter in some edge cases but for simple sentences it's just fine
my initial solution for this was so massovley overcomplicated for what it was
cool
my heart sank when i looked at the model solution
ty for helping btw
This was mine
!e
text = 'ab cd ef'
print(*[ch[0] for ch in text.split()], sep='\n')
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | a
002 | c
003 | e
when i first did it i did this
sentence = input("Please type in a sentence")
space = sentence.find(" ")
index = space
if " " not in sentence[0:space+1]:
print(sentence[0])
if " " in sentence[0:space+1]:
print(sentence[0])
print(sentence[space+1])
space+=1
while True:
if sentence[space] != " ":
space+=1
if sentence[space] == " ":
print(sentence[space+1])
space+=1
if " " not in sentence[space:] or " " not in (sentence[0:space+1]):
break
and then i look at the model solution and its one line
That's ok, it's fun to look back and see how far you've come
yeah, this is another way where you just see if the previous index is a space (and the current is NOT a space)
yeah, it's a really handy technique
and I find the lightswitch analogy helps people understand it better
yeah its definitely helpful ty once again
np!
will carry on coding tmrw , learnt that its better to do one or two exercises per day over a long period of time then to do 5 in a day get burnt out and procrastinate
I'd also make sure to revisit these past exercises
just like learning the piano, you don't learn a song once and move on
you go back and practice those songs
as in should I go back to exercises to try see how i could make it more efficient or ?
just to see if you can still solve it
ah okay
I see people write programs and then come back a week later and they couldn't remember how to do it
"Don't practice until you get it right. Practice until you can't get it wrong"
it's better when it's still fresh
the longer you wait, the more you'll lose it
I usually learn something by following along a guide/tutorial, then immediately opening a blank file and seeing if I can do it again without the guide this time
then I'll do it again tomorrow and see how much I remember
so tomorrow I can basically do the same thing for the two exercises I completed today
I always have this chat to refer to if i need help (I will still be trying to do without the help)
yup
tbh looking back at part 3 i still have 0 clue how i would do this exercise, they do have a couple of really weird ones like this
which section within part 3 is this
yeah some exercises are extremely specific and weird lol
yup
apart from the mooc did you try any other course
like cs50p?
no
fair, im assuming they all teach the same thing anyways so no need to do multiple
you know you could go on to learn a python framework maybe
i was thinking about going into cs50p for a refreshe though sinces ive been really lacking on actually coding
yeah that wouldn’t be bad
it would probably be quick to finish too since you already learnt most from mooc
the stuff im doing rn i barely understand
one question how comes your learning to program? is it for a specific career or just for fun
don’t mean to be all personal just curious
for me it’s both
its abit of both i guess
what stuff have you been doing recently
doing image processing in their dap course
but that course kinda sucks tbh
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
def center(a):
height, width = a.shape[:2]
return (height -1) / 2, (width -1 ) / 2
def radial_distance(a):
height, width = a.shape[:2]
x, y = center(a)
a, b = np.indices((height, width))
return np.sqrt((a-x)**2 + (b-y)**2)
def scale(a, tmin=0.0, tmax=1.0):
# """Returns a copy of array 'a' with its values scaled to be in the range
# [tmin,tmax]."""
amax, amin = np.max(a), np.min(a)
if amax != amin:
a_copy = (a-amin)/(amax-amin) * (tmax - tmin) + tmin
# tmin + (a-tmin)*(tmax-tmin)/(amax-amin)
else:
a_copy = np.zeros(a.shape)
return a_copy
def radial_mask(a):
distance = radial_distance(a)
mask = abs(1.0 - scale(distance))
row, col = (a.shape[0] -1) // 2, (a.shape[1] -1) // 2
mask[row, col] = 1.0
return mask
def radial_fade(a):
mask = radial_mask(a)
if a.ndim == 3:
mask = mask.reshape((a.shape[0], a.shape[1], 1)).astype(a.dtype)
return a * mask
def main():
a = plt.imread("painting.png")
mask = radial_mask(a)
faded = radial_fade(a)
fig, axes = plt.subplots(3, 1)
axes[0].imshow(a)
axes[1].imshow(mask, cmap = "gray")
axes[2].imshow(faded)
plt.show()
if __name__ == "__main__":
main()
this took multiple hours of bashing my head against a wall
and all it does is fade the outside of an image
no idea what you would use it for tbh
if you feel like the course is useless and u don’t enjoy it
i’d say just drop it
and u mentioned how ur interested in cybersec/ai
maybe look into how u could start with those
or learn a python framework
i should, but im stubborn so i may aswell finnish what ive started
anyway you should !close the thread now since you're done with the exercise
yup
!close
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

