试图改变字符串的功能

时间:2017-11-21 01:07:43

标签: python-3.x

我编写了这段代码,以便用户可以输入一个句子然后给出一个选项列表来选择编辑他们的句子或检查是否属实。我试图让它循环,直到用户拒绝看到另一个功能,但我不知道该怎么做。

如何制作以便用户可以决定是否继续查看列表?

def main():
  global string
  string = str(input("Write a sentence with both numbers and letters: "))
  print("There are many different options when it comes to string functions")
  print("Here is a list of options that you can use to edit your sentence:")
    title()
    lower()
    upper()
    islower()
    isupper()
    isspace()
    count()
    strip()
    replace()
    decimal()
    alpha()
    numeric()
    swap()

def title():  
  title = input("would you like to change the first letter of each word to UPPERCASE?")
  if title == "yes":
    print(string.title())

def lower():    
  lower = input("Would you like to change all characters to lowercase?")
  if lower == "yes":
    print(string.lower())

def upper():
  upper = input("Would you like to change all characters to UPPERCASE? ")
  if upper == "yes":
    print(string.upper())

def islower():
  islower = input("Would you like to check if your sentence is all lowercase? ")
  if islower == "yes":
    print(string.islower())

def isupper():
  isupper = input("Would you like to check if your sentence is all UPPERCASE? ")
  if isupper == "yes":
    print(string.isupper())

def isspace():
  isspace = input("Would you like to check if your sentence is only spaces? ")
  if isspace == "yes":
    print(string.isspace())

def count():
  count = input("Would you like to count the number of 'a' letters that are in your sentence? ")
  if count == "yes":
    print(string.count("a"))

  isdigit = input("Would you like to check if your sentence is all digits? ")
  if isdigit == "yes":
    print(string.isdigit())

def strip():
  strip = input("Would you like to take out the spaces before and after your sentence? ")
  if strip == "yes":
    print(string.strip(" "))

def replace():
  replace = input("Would you like to replace 'a' with 'X' in your sentence? ")
  if replace == "yes":
    print(string.replace("a", "X"))

def decimal():
  decimal = input("Would you like to check if your sentence is all decimals? ")
  if decimal == "yes":
    print(string.isdecimal())

def alpha():
  alpha = input("Would you like to check if your sentence is all letters from the alphabet? ")
  if alpha == "yes":
    print(string.isalpha())

def numeric():
  numeric = input("Would you like to check if your sentence is made up of numbers? ")
  if numeric == "yes":
    print(string.isnumeric())
def swap():
  swap = input("Would you like to swap the uppercase letters with the lowercase letters? ")
  if swap == "yes":
    print(string.swapcase())


main()  

2 个答案:

答案 0 :(得分:2)

你考虑过使用布尔标志吗?

程序开头的displayList = True之类的东西。然后如果他们输入他们不想再看到列表displayList = False,那么将列表包装在

if displayList:
    print("list details")

这样它只会在displayList为真时显示。

答案 1 :(得分:1)

如果用户输入" no"以外的任何内容,则您的main()函数会再次运行。

if let fragment = url.fragment{
  url = URL(string: url.absoluteString.replacingOccurrences(of: "#\(fragment)", with: "")!
}