Python Forum
How can I go back to a specific code line?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I go back to a specific code line?
#1
Hi people,

I am new here and I have an issue with one of the exercises of a course I am taking.

Actually, it's not a problem with the exercise, but I want to do something different and can't seem to figure out the way to do it.

Here is the code:

# 28.08.2018 - Phyton Shape Area Calculator #

name = raw_input("Hello, What is your name? ")
print 'Hello ' + name
print 'Area calculator is running'

shape = raw_input("What shape do you want to input? Please enter C for Circle or T for Triangle ")


if shape == 'C':
 radius = float(raw_input("Insert the radius of the circle: "))
 areac = 3.14159 * radius**2
 print 'The area of your circle is ' + str(areac)
  
elif shape == 'T':
  base = float(raw_input('What is the base of the triangle? '))
  height = float(raw_input('What is the height of your triangle? '))
  areat = 0.5 * base * height
  print 'The area of your triangle is ' + str(areat)
  
else:
  print 'Are you stupid?'
I just want the program to go back to the "What shape do you want to input? Please enter C for Circle or T for Triangle " if the user end up inputing the wrong letter (any letter but the 'C' or 'T').

Can anyone help me?

Thanks.
Reply
#2
Look into while loops.

To make it simple wrap your input line in a while and break out of it if the user enters an acceptable input.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to revert back to a previous line from user input Sharkenn64u 2 808 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Printing the code line number arbiel 6 1,563 Jun-30-2024, 08:01 AM
Last Post: arbiel
  Unable to understand the meaning of the line of code. jahuja73 0 1,004 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 2,754 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Code won't break While loop or go back to the input? MrKnd94 2 1,895 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Python code to read second line from CSV files and create a master CSV file sh1704 1 3,438 Feb-13-2022, 07:13 PM
Last Post: menator01
  how long can a line of code be? Skaperen 2 2,936 Jun-09-2021, 06:31 PM
Last Post: Skaperen
  Printing a specific line from a JSON serpiente 4 7,175 Mar-14-2021, 07:27 PM
Last Post: buran
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 4,475 Dec-06-2020, 10:10 PM
Last Post: Aizou
  Virtual Assistant code is looping back in python ShishirModi 1 3,387 Oct-13-2020, 08:04 AM
Last Post: Nickd12

Forum Jump:

User Panel Messages

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