def rec(string): if len(string)!=1: rec(string[1:]) print(string[0],end='') rec(input('string here:'))那这样的话倒数第二个怎么输出呢
时间: 2024-05-06 15:17:20 浏览: 12
你可以在递归函数中增加一个计数器,表示当前处理到字符串的第几个字符,当计数器等于倒数第二个字符的位置时,就输出它。以下是修改后的代码:
```python
def rec(string, count=0):
if count == len(string) - 2:
print(string[count], end='')
elif len(string) != 1:
rec(string[1:], count+1)
print(string[0], end='')
rec(input('string here:'))
```
这里增加了一个名为 `count` 的参数,表示当前处理到字符串的第几个字符。当 `count` 等于倒数第二个字符的位置时,就输出它。在递归调用中,将 `count` 加一传递给下一层递归函数。
相关问题
def rec(string): if len(string)!=1: rec(string[1:]) print(string[0],end='') rec(input('string here:'))
这是一个使用递归函数来实现字符串反转的代码。它会要求用户输入一个字符串,然后将字符串传递给递归函数rec(),该函数会对字符串进行处理并将结果返回给主函数。在递归函数中,如果字符串长度不为1,则递归调用rec()函数并传递字符串的子串string[1:],直到字符串长度为1。然后,递归函数会打印出原字符串的最后一个字符string[0],并在主函数中输出反转后的字符串。
<<<TimeString for format converting in 24-hour notation is 15:20 >>>Time in 12-hour notation is: 03:20 PM === <<<TimeString for format converting in 24-hour notation is 27:10 TimeFormatException: Invalid Value for Hour! === <<<TimeString for format converting in 24-hour notation is 16:78 TimeFormatException: Invalid Value for Minute! === <<<TimeString for format converting in 24-hour notation is abc TimeFormatException: Invalid Value for Time! === <<<TimeString for format converting in 24-hour notation is 6:30 >>>Time in 12-hour notation is: 06:30 AM End of program根据输出重新编写代码
```python
class TimeFormatException(Exception):
pass
def convert_time(time_string):
try:
hour, minute = time_string.split(":")
hour, minute = int(hour), int(minute)
if hour < 0 or hour > 23:
raise TimeFormatException("Invalid Value for Hour!")
if minute < 0 or minute > 59:
raise TimeFormatException("Invalid Value for Minute!")
if hour < 12:
am_pm = "AM"
if hour == 0:
hour = 12
else:
am_pm = "PM"
if hour > 12:
hour -= 12
return "{:02d}:{:02d} {}".format(hour, minute, am_pm)
except ValueError:
raise TimeFormatException("Invalid Value for Time!")
print("<<<TimeString for format converting in 24-hour notation is 15:20 >>>Time in 12-hour notation is:", convert_time("15:20"))
print("<<<TimeString for format converting in 24-hour notation is 27:10", end=" ")
try:
print(">>>TimeFormatException:", end=" ")
print(convert_time("27:10"))
except TimeFormatException as e:
print(e)
print("<<<TimeString for format converting in 24-hour notation is 16:78", end=" ")
try:
print(">>>TimeFormatException:", end=" ")
print(convert_time("16:78"))
except TimeFormatException as e:
print(e)
print("<<<TimeString for format converting in 24-hour notation is abc", end=" ")
try:
print(">>>TimeFormatException:", end=" ")
print(convert_time("abc"))
except TimeFormatException as e:
print(e)
print("<<<TimeString for format converting in 24-hour notation is 6:30 >>>Time in 12-hour notation is:", convert_time("6:30"))
print("End of program")
```
输出:
```
<<<TimeString for format converting in 24-hour notation is 15:20 >>>Time in 12-hour notation is: 03:20 PM
<<<TimeString for format converting in 24-hour notation is 27:10 >>>TimeFormatException: Invalid Value for Hour!
<<<TimeString for format converting in 24-hour notation is 16:78 >>>TimeFormatException: Invalid Value for Minute!
<<<TimeString for format converting in 24-hour notation is abc >>>TimeFormatException: Invalid Value for Time!
<<<TimeString for format converting in 24-hour notation is 6:30 >>>Time in 12-hour notation is: 06:30 AM
End of program
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)