Sep-30-2019, 04:28 PM
(This post was last modified: Sep-30-2019, 06:40 PM by newbieAuggie2019.)
Hi!
First, I made a small program to draw a poligon whose number of sides is entered by the user:
and it works:
![[Image: poligon-07-sides.png]](https://i.postimg.cc/SRsqXdQX/poligon-07-sides.png)
![[Image: poligon-12-sides.png]](https://i.postimg.cc/RhN5C0GK/poligon-12-sides.png)
Then I decided to complicate things a little more (going from simpler to more complex), by preventing the user to enter letters, negative numbers or decimal numbers (with a 'decimal separator' like '.' or ','), but then I have already encountered problems (I haven't coded yet from preventing entering letters). My program is now like this:
but when I checked with a few numbers, I get this:
Otherwise, if a number with no decimal separator is entered, line 18 would run:
and then, if the number is < '3' (lines 19-22), the program would keep asking until a number >= '3' is entered:
If the number entered is >= '3', line 24 would run:
then the inner block would run, lines 25-28:
So I don't understand why:
1) When I entered 2.5 (having '.' in the input) the code that is executed is lines 18-22:
instead of lines 12-16:
2) When I entered 3.5 (having '.' in the input) the code that is executed is lines 24-28:
instead of lines 12-16:
3) Regardless of my thoughts in question 2), why is that
4) I thought that Python runs linearly, I mean line 1 is run previously to line 2, and this line 2 previously to line 3, and so on. So am I wrong on this? That's also why I'm clueless about this problem .
Any ideas would be appreciated, thanks.
All the best,
First, I made a small program to draw a poligon whose number of sides is entered by the user:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# mawp_01_choosepoligon_01.py # # It works! from turtle import * speed( 0 ) nsides = input ("Enter the number \ (bigger than 2 ) of sides of your \ desired poligon.\n") ang1 = 360 / int (nsides) def poligon(sidelength = 100 ): for i in range ( int (nsides)): forward(sidelength) right(ang1) poligon() |
Output:Enter the number (bigger than 2) of sides of your desired poligon.
7
>>>
![[Image: poligon-07-sides.png]](https://i.postimg.cc/SRsqXdQX/poligon-07-sides.png)
Output:Enter the number (bigger than 2) of sides of your desired poligon.
12
>>>
![[Image: poligon-12-sides.png]](https://i.postimg.cc/RhN5C0GK/poligon-12-sides.png)
Then I decided to complicate things a little more (going from simpler to more complex), by preventing the user to enter letters, negative numbers or decimal numbers (with a 'decimal separator' like '.' or ','), but then I have already encountered problems (I haven't coded yet from preventing entering letters). My program is now like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# mawp_01_choosepoligon_02.py # # It doesn't work yet. from turtle import * speed( 0 ) nsides = input ("\nEnter the number \ (bigger than 2 ) of sides of your \ desired poligon.\n") while "." in nsides or "," in nsides: nsides = input ("\nDo you think a poligon can have half a side?\n\ Please, enter the number \ (bigger than 2 ) of sides of your \ desired poligon.\n") if "." not in nsides and "," not in nsides: while nsides < '3' : nsides = input ("\nPlease, enter the number \ (bigger than 2 ) of sides of your \ desired poligon.\n") if nsides > = '3' : ang1 = 360 / int (nsides) for i in range ( int (nsides)): forward( 75 ) right(ang1) |
Output:Enter the number (bigger than 2) of sides of your desired poligon.
1
Please, enter the number (bigger than 2) of sides of your desired poligon.
2
Please, enter the number (bigger than 2) of sides of your desired poligon.
-1
Please, enter the number (bigger than 2) of sides of your desired poligon.
0
Please, enter the number (bigger than 2) of sides of your desired poligon.
2.5
Please, enter the number (bigger than 2) of sides of your desired poligon.
3.5
Error:Traceback (most recent call last):
File "C:\Users\User1\AppData\Local\Programs\Python\Python37\mawp_01_choosepoligon_02.py", line 25, in <module>
ang1 = 360/int(nsides)
ValueError: invalid literal for int() with base 10: '3.5'
So I think something must be wrong with my logic in this program. I wrote the program, thinking that while a number with a decimal separator (a number having a '.' or a ',' covering the use of ',' in most countries in Europe), the program would keep asking until a number with no decimal separator is entered (lines 12-16):1 2 3 4 5 |
while "." in nsides or "," in nsides: nsides = input ("\nDo you think a poligon can have half a side?\n\ Please, enter the number \ (bigger than 2 ) of sides of your \ desired poligon.\n") |
1 |
if "." not in nsides and "," not in nsides: |
1 2 3 4 |
while nsides < '3' : nsides = input ("\nPlease, enter the number \ (bigger than 2 ) of sides of your \ desired poligon.\n") |
1 |
if nsides > = '3' : |
1 2 3 4 |
ang1 = 360 / int (nsides) for i in range ( int (nsides)): forward( 75 ) right(ang1) |
1) When I entered 2.5 (having '.' in the input) the code that is executed is lines 18-22:
1 2 3 4 5 |
if "." not in nsides and "," not in nsides: while nsides < '3' : nsides = input ("\nPlease, enter the number \ (bigger than 2 ) of sides of your \ desired poligon.\n") |
1 2 3 4 5 |
while "." in nsides or "," in nsides: nsides = input ("\nDo you think a poligon can have half a side?\n\ Please, enter the number \ (bigger than 2 ) of sides of your \ desired poligon.\n") |
1 2 3 4 5 |
if nsides > = '3' : ang1 = 360 / int (nsides) for i in range ( int (nsides)): forward( 75 ) right(ang1) |
1 2 3 4 5 |
while "." in nsides or "," in nsides: nsides = input ("\nDo you think a poligon can have half a side?\n\ Please, enter the number \ (bigger than 2 ) of sides of your \ desired poligon.\n") |
int(3.5)
produces an error? Shouldn't int(3.5)
produce '3'
?4) I thought that Python runs linearly, I mean line 1 is run previously to line 2, and this line 2 previously to line 3, and so on. So am I wrong on this? That's also why I'm clueless about this problem .
Any ideas would be appreciated, thanks.
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs