Sep-30-2019, 04:28 PM
Hi!
First, I made a small program to draw a poligon whose number of sides is entered by the user:
![[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:
1) When I entered 2.5 (having '.' in the input) the code that is executed is lines 18-22:
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:
# 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()and it works:
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:
# 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)but when I checked with a few numbers, I get this:
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):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")Otherwise, if a number with no decimal separator is entered, line 18 would run:
if "." not in nsides and "," not in nsides:and then, if the number is < '3' (lines 19-22), the program would keep asking until a number >= '3' is entered:
while nsides < '3': nsides = input("\nPlease, enter the number \ (bigger than 2) of sides of your \ desired poligon.\n")If the number entered is >= '3', line 24 would run:
if nsides >= '3':then the inner block would run, lines 25-28:
ang1 = 360/int(nsides) for i in range(int(nsides)): forward(75) right(ang1)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:
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")instead of lines 12-16:
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")2) When I entered 3.5 (having '.' in the input) the code that is executed is lines 24-28:
if nsides >= '3': ang1 = 360/int(nsides) for i in range(int(nsides)): forward(75) right(ang1)instead of lines 12-16:
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")3) Regardless of my thoughts in question 2), why is that
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,