Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple lines?
#1
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.
Reply
#2
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')
Reply
#3
(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.
Reply
#4
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
Reply
#5
Compare
response = input("What is your radius? ")
with
response = ("What is your radius? ")
do you see the difference?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write the condition for deleting multiple lines? Lky 3 1,137 Jul-10-2022, 02:28 PM
Last Post: Lky
  Delete multiple lines from txt file Lky 6 2,292 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Display table field on multiple lines, 'wordwrap' 3python 0 1,770 Aug-06-2021, 08:17 PM
Last Post: 3python
  pulling multiple lines from a txt IceJJFish69 3 2,575 Apr-26-2021, 05:56 PM
Last Post: snippsat
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,834 Aug-10-2020, 11:01 PM
Last Post: medatib531
  print python json dump onto multiple lines lhailey 2 19,850 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  Random nr. no repetition & printing multiple lines Joey 7 2,795 Feb-05-2020, 04:23 PM
Last Post: Larz60+
  how to insert # to multiple lines? hkfatasy 1 2,895 Dec-22-2019, 01:51 AM
Last Post: ichabod801
  KeyError -read multiple lines bongielondy 2 3,450 Nov-06-2019, 01:33 AM
Last Post: bongielondy
  spliting similar but multiple lines anna 7 3,561 Apr-20-2019, 08:53 AM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020