2017-11-21 6 views
1

私はこのコードを書いてユーザーが文章を入力し、その文章を編集したり、事実が真であるかどうかをチェックするためのオプションリストを与えたりできました。私は、ユーザーが別の機能を見ていないと言うまでループを作ろうとしていますが、どうやってそれを行うのか分かりません。文字列の機能を変更しようとしています

リストを見続けるかどうかをユーザーが決定できるようにするにはどうすればよいですか?

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() 
+0

ユーザーは入力したい情報を使用して、ユーザーが何を望んでいるかを確認することができます –

答えて

1

ユーザーが「いいえ」以外を入力すると、main()関数が再び実行されます。

exit = "" 
while(exit != "no"): 
    main() 
    print("See options again?") 
    exit = input() 
+0

どこに置くの? – savannah

+0

関数を定義したら、最後にそのmain()をこのコードに置き換えます –

2

ブール値フラグを使用したことはありますか?

プログラムの開始時に、displayList = Trueのようなものがあります。 displayListがtrueの場合、それだけ表示されるように、その後、彼らはもうリストを表示したくないことを、彼らは入力、displayList = False、場合は

if displayList: 
    print("list details") 

でリストをラップ。

+0

私はどこにプログラムを配置しますか? – savannah

関連する問題