Hello, everyone. I'm fairly new to coding and in a bit of a jam. I'm trying to type out this example i saw online on IDLE and it reads something like this.
1 response = input("What is your radius? ")
2 r = float(response)
3 area = 3.14159 * r ** 2
4 print("The area is ", area)
But MY code stops at the 2nd line.
>>> response = input("What is your radius?")
What is your radius?
What am I doing wrong or how do i get my code to look something like the example? And how come I can't code offline? Any help is
greatly appreciated.
the code will break on line 2
if response contains anything other than a number,
including an empty line.
you can use:
while True:
response = input("What is your radius? ")
if response.isdigit():
break
print('Please enter a number')
(Nov-15-2017, 12:49 AM)Larz60+ Wrote: [ -> ]the code will break on line 2
if response contains anything other than a number,
including an empty line.
you can use:
while True:
response = input("What is your radius? ")
if response.isdigit():
break
print('Please enter a number')
How did you get it to go to the next line without executing the first line? the first code i posted is supposed to work but i cant go to the next line. I'm not sure if im making any sense. What are you using to code? If you copy and paste the first code
response = input("What is your radius? ")
r = float(response)
area = 3.14159 * r ** 2
print("The area is ", area)
and press enter. It works fine. I just cant get to the next line in IDLE. If i type
>>> response = ("What is your radius? ")
>>> r = float(response)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
r = float(response)
ValueError: could not convert string to float: 'What is your radius? '
>>>
I get that error. I'm not sure what im doing wrong and why the first code from the example online works and i cant type it out on IDLE like the example.
Quote:and press enter. It works fine. I just cant get to the next line in IDLE. If i type
Not if you input text, it will still crash
the way I wrote it:
while True:
this starts an endless loop. It says execute all following indented code, forever.
the input statement is the same:
response = input("What is your radius? ")
nothing special here.
the next line:
if response.isdigit():
break
says 'If the value i response is numeric, I've got what I want, so break the endless loop'
and does just that.
If it is not numeric, it continues on to the next line, printing the message
print('Please enter a number')
And restarts the loop
Compare
response = input("What is your radius? ")
with
response = ("What is your radius? ")
do you see the difference?