Python case语句中的用户输入问题
我在运行案例切换代码时遇到问题这是我第一次运行案例切换。每当我在python上运行它时,它只允许用户为1选择与2和3相关的问题,它只是打印消息,在那里它要求用户输入。可能有一个非常简单的解决办法,我错过了Python case语句中的用户输入问题,python,switch-statement,Python,Switch Statement,我在运行案例切换代码时遇到问题这是我第一次运行案例切换。每当我在python上运行它时,它只允许用户为1选择与2和3相关的问题,它只是打印消息,在那里它要求用户输入。可能有一个非常简单的解决办法,我错过了 userchoice=1 while userchoice!=0: userchoice = input("Press 1 or 2 or 3 to run solution for 1 or 2 or 3 or press 0 to exit: ")
userchoice=1
while userchoice!=0:
userchoice = input("Press 1 or 2 or 3 to run solution for 1 or 2 or 3 or press 0 to exit: ")
if int(userchoice)==1:
# Copy your code for question 1 here
#Question 1
def main():
#String input
s=input('Enter Random String of numbers and letters: ')
#Calculating string of numbers
str1=""
for i in range(0,len(s)):
str=s[i]
if(str.isnumeric()==True):
str1+=str
print("String of Numbers is: ", str1)
#Calculating longest substring in descending order
str2=""
str2+=str1[0]
max_string=str2
length=1
max_length=1
for i in range(1,len(str1)):
if str1[i]>str1[i-1]:
length+=1
str2+=str1[i]
if length>max_length:
max_length=length
max_string=str2
else:
length=1
str2=str1[i]
print("Longest substring in numberic descending order is", max_string)
#Calculating the Average of numbers
avg=0.0
for i in range(0, len(max_string)):
avg+=float(max_string[i])
avg=avg/max_length
print("Average: ", round(avg,0))
if __name__=="__main__":
main()
elif int(userchoice)==2:
#Encryption section:
text=input("Enter a phrase for encryption (lowercase, no spaces):" ) #instructions that will assist the user to meet the input requirements for the code to work
dis=int(input("Enter distance value: ")) #dis is the number of spaces the user wishes the letter to be moved for encryption (example if dis is 5 and the phrase is "cat", "cat" will become "hfy")
code=" "
for ch in text:
ordvalue=ord(ch) #the ordvalue is defined as ord(ch), ord takes the string argument of a single unicode character and returns its point value
ciphervalue=ordvalue+dis #the ciphervalue takes the point value of the characters in the selected phrase and adds the numberic value inputted by the user to each characters point value
if ciphervalue>ord('z'): #defines rules for if the ciphervalue is grater then the unit value of 'z'
x=ciphervalue-ord('z') #uses the constant 'x' defining it as the ciphervalue minus the value of 'z'
ciphervalue=ord('a')+x-1 #if the value is greater then 'z' then the cipher value is the unit value of 'a' plus what is defined to be 'x'
code=code+chr(ciphervalue) #uses the 'chr' function to convert the interger collected by the ciphervalue and converts it to the character that occupies that value
print("Encription is: ", code) #Prints the encrypted values
#Decryption
decrypt=input("Enter a phrase for decryption (lowercase, no spaces): ") #instructions that will assist the user to meet the input requirements for the code to work
dis=int(input("Enter distance value: ")) #number of spaces the user wishes to move the letter to decrypt the code (example, encryption: "hfy", distance:5, decryption= cat )
code=" "
for ch in decrypt:
ordvalue=ord(ch) #similar to decryption
ciphervalue=ordvalue-dis #instead of adding the users numeric input to the unit value this takes the value inputted by the user
if ciphervalue<ord('a'): #rules for if the ciphervalue results in a value less then the unit value of 'a'
x=ord('a')-ciphervalue #defines 'x' in a similar way to the encryption
ciphervalue=ord('z')-x+1
code=code+chr(ciphervalue)
print("decryption is:", code)
# Copy your code for question 2 here
elif int(userchoice)==3:
#Question 3
count = 0
total = 0.0
filename = input("Enter file name: ")
x = open(filename)
for line in x:
if not line.startswith("X-DSPAM-Confidence:") :
continue
t=line.find("0")
number=float(line[t: ])
count = count+1
total = total + number
average = total/count
print("Average spam confidence: ", average)
elif int(userchoice)==0:
print("Thanks!")
break
else:
print("Wrong Choice, Please try 1,2,3 or press 0 to exit!")
userchoice=1
而用户选择=0:
userchoice=input(“按1或2或3运行1或2或3的解决方案,或按0退出:”)
如果int(userchoice)==1:
#在这里复制问题1的代码
#问题1
def main():
#字符串输入
s=输入('输入随机数字和字母字符串:')
#计算一串数字
str1=“”
对于范围(0,len(s))内的i:
str=s[i]
如果(str.isnumeric()==True):
str1+=str
打印(“数字字符串为:”,str1)
#按降序计算最长子串
str2=“”
str2+=str1[0]
最大字符串=str2
长度=1
最大长度=1
对于范围(1,len(str1))中的i:
如果str1[i]>str1[i-1]:
长度+=1
str2+=str1[i]
如果长度>最大长度:
最大长度=长度
最大字符串=str2
其他:
长度=1
str2=str1[i]
打印(“按数字降序排列的最长子字符串为”,最大字符串)
#计算数字的平均值
平均值=0.0
对于范围内的i(0,len(最大字符串)):
平均+=浮点(最大字符串[i])
平均值=平均值/最大长度
打印(“平均:”,圆形(平均值,0))
如果名称=“\uuuuu main\uuuuuuuu”:
main()
elif int(用户选择)=2:
#加密部分:
text=input(“输入加密短语(小写,无空格):”)#说明将帮助用户满足代码工作的输入要求
dis=int(输入(“输入距离值”)#dis是用户希望移动字母进行加密的空格数(例如,如果dis为5,短语为“cat”,“cat”将变为“hfy”)
code=“”
对于文本中的ch:
ordvalue=ord(ch)#ordvalue定义为ord(ch),ord接受单个unicode字符的字符串参数并返回其点值
ciphervalue=ordvalue+dis#ciphervalue获取所选短语中字符的点值,并将用户输入的数字值添加到每个字符点值中
如果ciphervalue>ord('z'):#定义了规则,如果ciphervalue大于'z'的单位值
x=ciphervalue ord('z')#使用常量'x'将其定义为ciphervalue减去'z'的值
ciphervalue=ord('a')+x-1#如果值大于'z',则密码值为'a'的单位值加上定义为'x'的值
code=code+chr(ciphervalue)#使用'chr'函数转换ciphervalue收集的整数,并将其转换为占用该值的字符
打印(“描述为:”,代码)#打印加密值
#解密
decrypt=input(“输入一个用于解密的短语(小写,无空格):”)#将帮助用户满足代码工作所需的输入要求的说明
dis=int(输入(“输入距离值”)#用户希望移动字母以解密代码的空格数(例如,加密:“hfy”,距离:5,解密=cat)
code=“”
对于ch in解密:
ordvalue=ord(ch)#类似于解密
ciphervalue=ordvalue dis#不是将用户的数字输入添加到单位值,而是采用用户输入的值
如果ciphervalue您正在选项1内创建函数main,但从未调用该函数
if __name__ == "__main__"
是脚本的入口点,不过在本例中,您要做的是直接调用主函数。您可以这样尝试:
if int(userchoice) == 1:
# Copy your code for question 1 here
# Question 1
def main():
# String input
s = input('Enter Random String of numbers and letters: ')
# Calculating string of numbers
str1 = ""
for i in range(0, len(s)):
str = s[i]
if (str.isnumeric() == True):
str1 += str
print("String of Numbers is: ", str1)
# Calculating longest substring in descending order
str2 = ""
str2 += str1[0]
max_string = str2
length = 1
max_length = 1
for i in range(1, len(str1)):
if str1[i] > str1[i - 1]:
length += 1
str2 += str1[i]
if length > max_length:
max_length = length
max_string = str2
else:
length = 1
str2 = str1[i]
print("Longest substring in numberic descending order is", max_string)
# Calculating the Average of numbers
avg = 0.0
for i in range(0, len(max_string)):
avg += float(max_string[i])
avg = avg / max_length
print("Average: ", round(avg, 0))
main()
if int(userchoice) == 1:
# Copy your code for question 1 here
# Question 1
# String input
s = input('Enter Random String of numbers and letters: ')
# Calculating string of numbers
str1 = ""
for i in range(0, len(s)):
str = s[i]
if (str.isnumeric() == True):
str1 += str
print("String of Numbers is: ", str1)
# Calculating longest substring in descending order
str2 = ""
str2 += str1[0]
max_string = str2
length = 1
max_length = 1
for i in range(1, len(str1)):
if str1[i] > str1[i - 1]:
length += 1
str2 += str1[i]
if length > max_length:
max_length = length
max_string = str2
else:
length = 1
str2 = str1[i]
print("Longest substring in numberic descending order is", max_string)
# Calculating the Average of numbers
avg = 0.0
for i in range(0, len(max_string)):
avg += float(max_string[i])
avg = avg / max_length
print("Average: ", round(avg, 0))
或者,您可以直接将代码放在选项1块中主函数的内部,如下所示:
if int(userchoice) == 1:
# Copy your code for question 1 here
# Question 1
def main():
# String input
s = input('Enter Random String of numbers and letters: ')
# Calculating string of numbers
str1 = ""
for i in range(0, len(s)):
str = s[i]
if (str.isnumeric() == True):
str1 += str
print("String of Numbers is: ", str1)
# Calculating longest substring in descending order
str2 = ""
str2 += str1[0]
max_string = str2
length = 1
max_length = 1
for i in range(1, len(str1)):
if str1[i] > str1[i - 1]:
length += 1
str2 += str1[i]
if length > max_length:
max_length = length
max_string = str2
else:
length = 1
str2 = str1[i]
print("Longest substring in numberic descending order is", max_string)
# Calculating the Average of numbers
avg = 0.0
for i in range(0, len(max_string)):
avg += float(max_string[i])
avg = avg / max_length
print("Average: ", round(avg, 0))
main()
if int(userchoice) == 1:
# Copy your code for question 1 here
# Question 1
# String input
s = input('Enter Random String of numbers and letters: ')
# Calculating string of numbers
str1 = ""
for i in range(0, len(s)):
str = s[i]
if (str.isnumeric() == True):
str1 += str
print("String of Numbers is: ", str1)
# Calculating longest substring in descending order
str2 = ""
str2 += str1[0]
max_string = str2
length = 1
max_length = 1
for i in range(1, len(str1)):
if str1[i] > str1[i - 1]:
length += 1
str2 += str1[i]
if length > max_length:
max_length = length
max_string = str2
else:
length = 1
str2 = str1[i]
print("Longest substring in numberic descending order is", max_string)
# Calculating the Average of numbers
avg = 0.0
for i in range(0, len(max_string)):
avg += float(max_string[i])
avg = avg / max_length
print("Average: ", round(avg, 0))
可以使用以下函数定义模拟switch语句:
def switch(v): yield lambda *c: v in c
您可以在C样式中使用它:
for case in switch(int(userchoice)):
if case(1):
# ... you code for userchoice 1
break
if case(2):
# ... you code for userchoice 2
break
if case(3):
# ... you code for userchoice 3
break
if case(0):
# ... you code for userchoice 0
break
else:
# ... your code for bad userchoice
您还可以使用if/elif/else模式,而不使用中断:
for case in switch(int(userchoice)):
if case(1):
# ... you code for userchoice 1
elif case(2):
# ... you code for userchoice 2
elif case(3):
# ... you code for userchoice 3
elif case(0):
# ... you code for userchoice 0
else:
# ... your code for bad userchoice
艾丽斯·史密斯如果这解决了你的问题,你能这样做吗?非常感谢你,我知道我会错过一些简单的东西:P