简体   繁体   中英

How to ask user 'Do You Want To Continue?' in python using while loop but not by calling functions

[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

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")

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

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM