Python 如何从原始输入字符串值创建循环
我试图用我的程序来做的是要求用户输入一个输入字符串,该字符串稍后将使用str.upper或str.lower转换为大写或小写 我有5套选项供用户选择:Python 如何从原始输入字符串值创建循环,python,loops,Python,Loops,我试图用我的程序来做的是要求用户输入一个输入字符串,该字符串稍后将使用str.upper或str.lower转换为大写或小写 我有5套选项供用户选择: a = 'convert to upper case' b = 'convert to lower case' c = 'switch case of every alphabetic character to the opposite case' d = 'convert first and last chrs of each word to
a = 'convert to upper case'
b = 'convert to lower case'
c = 'switch case of every alphabetic character to the opposite case'
d = 'convert first and last chrs of each word to upper case, and others to lower'
e = 'no change'
到目前为止,我已经完成了选项a和b的转换。但在我继续为选项c、d和e创建代码之前。我试图创建一个循环,但我不知道如何使用原始输入和字符串来创建循环
这是我目前掌握的代码:
# Conversion Rules
a = 'convert to upper case'
b = 'convert to lower case'
c = 'switch case of every alphabetic character to the opposite case'
d = 'convert first and last chrs of each word to upper case, and others to lower'
e = 'no change'
def upper():
print 'Your Input: %s' % choice
print 'Choosen Conversion Rule: %s' % a
return 'Conversion Result: %s' % option_A
def lower():
print 'Your Input: %s' % choice
print 'Choosen Conversion Rule: %s' % b
return 'Conversion Result: %s' % option_B
choice = str(raw_input('Choose an Option:'))
if (choice == 'A') or (choice == 'a'):
value_A = str(raw_input('Enter a String to Convert:'))
option_A = str.upper(Value_A)
print upper()
elif (choice == 'B') or ('b'):
value_B = str(raw_input('Enter a String to Convert:'))
option_B = str.lower(value_B)
print lower()
else:
print 'Goodbye' # Here I want to break if 'Q' is entered if 'Q' is entered.
因此,在用户输入一个选项之后。例如“A”或“A”。第一个条件将运行,但是我想添加一个循环,返回到代码的开头,允许用户再次输入选项,或者选择一个不同的选项,以便运行不同的条件
choice = str(raw_input('Choose an Option:'))
if (choice == 'A') or (choice == 'a'):
value_A = str(raw_input('Enter a String to Convert:'))
option_A = str.upper(Value_A)
print upper()
# I want to add a loop here to go back to the 'choice' variable.
您可以将所有用户界面放在一个while循环中,该循环将永远循环(例如,直到按下某个键) 请注意,“中断”是将您从循环中打断的部分。由于用户界面部分在while循环中,它将重复
# Conversion Rules
a = 'convert to upper case'
b = 'convert to lower case'
c = 'switch case of every alphabetic character to the opposite case'
d = 'convert first and last chrs of each word to upper case, and others to lower'
e = 'no change'
def upper():
print 'Your Input: %s' % choice
print 'Choosen Conversion Rule: %s' % a
return 'Conversion Result: %s' % option_A
def lower():
print 'Your Input: %s' % choice
print 'Choosen Conversion Rule: %s' % b
return 'Conversion Result: %s' % option_B
while True:
choice = str(raw_input('Choose an Option:'))
if (choice == 'A') or (choice == 'a'):
value_A = str(raw_input('Enter a String to Convert:'))
option_A = str.upper(Value_A)
print upper()
elif (choice == 'B') or ('b'):
value_B = str(raw_input('Enter a String to Convert:'))
option_B = str.lower(value_B)
print lower()
else:
print 'Goodbye' # Here I want to break if 'Q' is entered if 'Q' is entered.
break