Python 使用ord()和char()的邮政分拣代码程序--调试故障
我的代码遇到了一个问题,让用户输入一系列数字/字母,程序使用提供的参数返回所有可行的邮政编码。具体问题如下: 编写一个从用户处读取数字的程序。你的程序 应能够跟踪/计算最小值、最大值、总值 和,以及输入的数字的平均值。当用户输入“q”时, “Q”或“quit”,程序应显示最小值、最大值和总值 结束前输入的数字的总和和平均值。你可以假设 用户将始终输入一个数字或一个字符串 “q”、“q”或“退出”。您还可以假定用户总是在 至少一个数字(即,他们不会立即选择退出 启动程序) 将Python程序保存在名为a2q3.py的文件中,并将其添加到 提交zip文件。问题4(邮政分拣代码) 正向分拣区号是分拣的前三个字符 加拿大邮政编码(例如,邮政编码“K1S 5B6”中的“K1S”)及其 格式如下:Python 使用ord()和char()的邮政分拣代码程序--调试故障,python,Python,我的代码遇到了一个问题,让用户输入一系列数字/字母,程序使用提供的参数返回所有可行的邮政编码。具体问题如下: 编写一个从用户处读取数字的程序。你的程序 应能够跟踪/计算最小值、最大值、总值 和,以及输入的数字的平均值。当用户输入“q”时, “Q”或“quit”,程序应显示最小值、最大值和总值 结束前输入的数字的总和和平均值。你可以假设 用户将始终输入一个数字或一个字符串 “q”、“q”或“退出”。您还可以假定用户总是在 至少一个数字(即,他们不会立即选择退出 启动程序) 将Python程序保存在
- 第一个字符:a和Z之间的字母
- 中间字符:介于0和9之间的数字,包括0和9。最后一个字符:介于a和Z之间的字母
start_let_first_char=input("Please enter the starting letter of the first character: ")
end_let_first_char=input("Please enter the ending letter of the first character: ")
start_dig_mid=int(input("Please enter the starting digit of the middle character: "))
end_dig_mid=int(input("Please enter the ending digit of the middle character: "))
start_let_last_char=input("Please enter the starting letter of the last character: ")
end_let_last_char=input("Please enter the ending letter of the last character: ")
for x in range(ord(start_let_first_char),ord(end_let_first_char)+1):
for y in range(start_dig_mid,end_dig_mid +1):
for z in range(ord(start_let_last_char),ord(end_let_last_char)+1):
print(chr(x)+str(y)+chr(z))
当我运行代码并输入所有字母/数字时,不断出现的错误是:
for z in range(ord(start_let_last_char,ord(end_let_last_char)+1)):
TypeError: ord() takes exactly one argument (2 given)
谢谢,它已经修好了,可以工作了!我忘了为邮政编码加一个计数器。我应该在末尾添加计数器和打印(计数器)功能,还是有人可以推荐更好的方法
for z in range(ord(start_let_last_char,ord(end_let_last_char)+1)):
应该是:
for z in range(ord(start_let_last_char),ord(end_let_last_char)+1):
那里错放了一个逗号
加上计数器:
letter_1_start = input(
"Please enter the starting letter of the first character: ")
letter_1_end = input(
"Please enter the ending letter of the first character: ")
digit_start = int(
input("Please enter the starting digit of the middle character: "))
digit_end = int(
input("Please enter the ending digit of the middle character: "))
letter_2_start = input(
"Please enter the starting letter of the last character: ")
letter_2_end = input(
"Please enter the ending letter of the last character: ")
counter = 0
for x in range(ord(letter_1_start), ord(letter_1_end) + 1):
for y in range(digit_start, digit_end + 1):
for z in range(ord(letter_2_start), ord(letter_2_end) + 1):
print(chr(x) + str(y) + chr(z))
counter += 1
print("Total number of codes:", counter)
关于错误消息还有什么不清楚的地方?你给了两个参数给ord,你希望它和它们有什么关系?你错放了a),我想,它应该是:对于范围内的z(ord(start\u let\u last\u char),ord(end\u let\u last\u char)+1):@jornsharpe抱歉。。。。我可以读到它需要一个论点,而我给出了两个论点,但我不确定如何解决这个问题/为什么我尝试的东西不起作用。你能给我指出正确的方向吗?@sphericalcowboy谢谢!!!!这就解决了!没问题。如果这解决了问题,您可能希望选择我的答案。