-4

[enter image description here][1]I'm new to this language, I'm not getting how should I ask user 'Do You Want To Continue?' using while loop but not calling the function inside this loop. For example:

do{
__some code here__
}while (ch == 'y')

I want this same in code python by without calling function

Screenshot: https://i.stack.imgur.com/ryy8k.png

Rikh
  • 4,078
  • 3
  • 15
  • 35
Munna Michael
  • 1
  • 1
  • 1
  • 3
  • How would you do it with a function? – Mad Physicist Jan 24 '19 at 19:26
  • please add a python tag to your question – Mad Physicist Jan 24 '19 at 19:27
  • 1
    Please provide the code as you would write it with functions for reference. This smells like homework and like you didn't even research the basic syntax of the language. Anyhow, as a new user, please take the [tour] and read [ask]. – Ulrich Eckhardt Jan 24 '19 at 19:32
  • [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) – martineau Jan 24 '19 at 19:51
  • @MadPhysicist I have attached the screenshot, please do understand my question, and help me out. – Munna Michael Jan 24 '19 at 19:52
  • `while True: a = input("Enter yes/no to continue") if a=="yes": gameplay() continue elif a=="no": break else: print("Enter either yes/no")` @UlrichEckhardt I have read this over stack overflow only, my query is to not to use function within a loop – Munna Michael Jan 24 '19 at 20:01
  • 1
    Please edit your question with code – Mad Physicist Jan 24 '19 at 21:04

4 Answers4

3

Since Python has no do-while construct, the equivalent would be an infinite loop with a condition that breaks it:

while True:
    # some code here
    if input('Do You Want To Continue? ') != 'y':
        break
blhsing
  • 91,368
  • 6
  • 71
  • 106
2

You can use input within the while statement condition check. Something like this:

while input("Do You Want To Continue? [y/n]") == "y":
    # do something
    print("doing something")
elahav
  • 51
  • 2
  • 4
0

In your code, all you have to do is to indent the last 3 lines inside the outer while loop, like this...

endFlag = False

while endFlag == False:
    i, l = 10, []

    while i < 5:
        a = int(input('Sub Marks'))
        l.append(a)
        i += 1

    total = sum(l)
    print('Total is: ', total)

    per = total*0.25
    print('Percentage: ',per)

    ui = input("Would you like to repeat the program again? Y/N")
    endFlag = True if ui.lower() == 'n' else False

Besides that, I found some ways to improve your code, that's why I modified it

MK14
  • 481
  • 3
  • 9
-1
import math
print("all formulas used from surface area and volume chapter maths class 10 21-22 batch")
name=input("enter your name: ")
while True:
  bhai="-commands-""\n Curved Suface Area of cylinder= csacy \n Total surface area of cylinder= tsacy \n Volume of cylinder= volcy \n \n Curved Suface Area of a cone= csaco \n \
Total Suface Area of cone= tsaco \n Volume of cone= volco \n \n Lateral surface area of cuboid= lsacu \n Total suface area of cuboid= tsacu \n Volume of cuboid= volcu \n \n Lat\
eral surface area of the cube= lsacb \n Total surface area of the cube= tsacb \n Volume of the cube= volcb \n \n Area of the sphere= aresph \n Volume of the sphere= volsph \n \\
n Curved surface area of the hemisphere= lsahem \n Total surface area of the hemisphere= tsahem \n Volume of the hemisphere= volhem"
  print()
  print(bhai)
  dib= input("\n enter command: ")
  if dib=="csacy":
      h= float(input("enter cylinder height(in cm: "))
      r= float(input("enter cylinder radius(in cm: "))
      π=22/7
      print("The curved surface area of the cylinder will be", float(2*π*r*h), "sq.cm")
  if dib=="tsacy":
      h= float(input("enter cylinder height(in cm)= "))
      r= float(input("enter cylinder radius(in cm)=  "))
      π=22/7
      print("The total surface area of the cylinder will be", float(2*π*r*(r+h)), "sq.cm")
  if dib=="volcy":
      h=float(input("enter cylinder height(in cm)= "))
      r=float(input("enter cylinder radius(in cm)= "))
      π=22/7
      print("The volume of the cylinder will be", float(π*r*r*h), "cube.cm")
  if dib=="csaco":
      r= float(input("enter cone radius(in cm)="))
      sl=float(input("enter cone slant height(in cm)= "))
      π=22/7
      print("The curved surface area of the cone will be", float(π*r*sl), "sq.cm")
  if dib=="tsaco":
      r= float(input("enter cone radius(in cm)= "))
      sl=float(input("enter cone slant height(in cm)= "))
      π=22/7
      print("The total surface area of the cone will be", float(π*r*(r+sl)), "sq.cm")
  if dib=="volco":
      h=float(input("enter cone height(in cm)= "))
      r= float(input("enter cone radius(in cm)= "))
      π=22/7
      print("The volume of the cone will be", float(1/3*π*r*r*h), "cube.cm")
  if dib=="lsacu":
      l=float(input("enter cuboid length(in cm)= "))
      h=float(input("enter cuboid height(in cm)= "))
      b=float(input("enter cuboid breadth(in cm)= "))
      print("The lateral surface area of the cuboid will be", float(2*h*(l+b)), "sq.cm")
  if dib=="tsacu":
      l=float(input("enter cuboid length(in cm)= "))
      h=float(input("enter cuboid height(in cm)= "))
      b=float(input("enter cuboid breadth(in cm)="))
      print("The total surface area of the cuboid will be", float(2*(l*b+b*h+h*l)), "sq.cm")
  if dib=="volcu":
      l=float(input("enter cuboid length(in cm)= "))
      h=float(input("enter cuboid height(in cm)= "))
      b=float(input("enter cuboid breadth(in cm)= "))
      print("The volume of the cuboid will be", float(l*b*h), "cube.cm")
  if dib=="lsacb":
      l=float(input("enter cube side length(in cm)= "))
      print("The lateral surface area of the cube will be", float(4*l*l), "sq.cm")
  if dib=="tsacb":
      l=float(input("enter cube side length(in cm)= "))
      print("The total surface area of the cube will be", float(6*l*l), "sq.cm")
  if dib=="volcb":
      l=float(input("enter cube side length(in cm)= "))
      print("The volume of the cube will be", float(l*l*l), "cube.cm")
  if dib=="aresph":
      r=float(input("enter sphere radius(in cm)= "))
      π=22/7
      print("The area of the sphere will be", float(4*π*r*r), "sq.cm")
  if dib=="volsph":
      r=float(input("enter sphere radius(in cm)= "))
      π=22/7
      print("The volume the sphere will be", float(4/3*π*r*r*r), "cube.cm")
  if dib=="lsahem":
      r=float(input("enter hemisphere radius(in cm)= "))
      π=22/7
      print("The lateral surface area of the hemisphere will be", float(2*π*r*r), "sq.cm")
  if dib=="tsahem":
      r=float(input("enter hemisphere radius(in cm)= "))
      π=22/7
      print("The total surface area of the hemisphere will be", float(3*π*r*r), "sq.cm")
  if dib=="volhem":
      r=float(input("enter hemisphere radius(in cm)="))
      π=22/7
      print("The volume of the hemisphere will be", float(2/3*π*r*r*r), "cube.cm")
  dy=input("\nDo you want to continue? [yes/no] : ")
  if dy=="no":
    break
  else:
    pass
print("\n Thank you", name, "\n Powered by chou"
chro
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 04 '22 at 17:38