如何修复 11、12 和 13 以“th”结尾





代码将输入一条语句,然后输入一个每次循环加 1 的数字,并根据语句的结尾输入“st of all”、“nd of all”、“rd of all”或“th of all”。号码。**当到达 11、12 和 13 时,它只使用“st of all”、“nd of all”和“rd of all”,而对于 11、12 和 13 应该只使用“th of all” .**


import pyautogui

import time


time.sleep(5)


number = 0

numberSuffix = ""


while True:


    number += 1


    if str(number).endswith("1"):

        numberSuffix = "st of all"

    if str(number).endswith("2"):

        numberSuffix = "nd of all"

    if str(number).endswith("3"):

        numberSuffix = "rd of all"

    if str(number).endswith("4" or "5" or "6" or "7" or "8" or "9" or "0" or "11" or "12" or "13"):

        numberSuffix = "th of all"


    print(str(number) + numberSuffix)


    pyautogui.typewrite("Statement")

    time.sleep(0.5)

    pyautogui.press("enter")

    time.sleep(0.5)

    pyautogui.typewrite(str(number) + numberSuffix)

    time.sleep(0.5)

    pyautogui.press("enter")

    time.sleep(0.5)


富国沪深
浏览 147回答 3
3回答

哔哔one

这是因为 11,12,13 的 if 语句是最后一个,尝试将最后一个 if 语句放在第一个 if 语句(当前 if 语句之上),看看会发生什么,更改后可能会出现其他一些边缘情况,但它会让你知道你做错了什么if str(number).endswith("4" or "5" or "6" or "7" or "8" or "9" or "0" or "11" or "12" or "13"):    numberSuffix = "th of all"elif str(number).endswith("1"):    numberSuffix = "st of all"elif str(number).endswith("2"):    numberSuffix = "nd of all"elif str(number).endswith("3"):    numberSuffix = "rd of all"我建议至少为此做一个定义

小唯快跑啊

该行没有执行您想要执行的操作:&nbsp;if str(number).endswith("4" or "5" or "6" or "7" or "8" or "9" or "0" or "11" or "12" or "13"):这里发生的事情"4" or "5" or "6" or ...只是被评估为"4"。来吧,你自己尝试一下。您实际上需要在这里做的事情更像是:if str(number).endswith("4") or str(number).endswith("5") or ...这显然需要输入很多额外的内容。执行 @Aplet123 建议的操作并检查实际数字而不是数字的字符串转换可能要容易得多。last_digit = number % 10last_two_digits = number % 100if 10 < last_two_digits < 14&nbsp; &nbsp; numberSuffix = "th of all"elif last_digit == 1:&nbsp; &nbsp; numberSuffix = "st of all"elif last_digit == 2:&nbsp; &nbsp; numberSuffix = "nd of all"elif last_digit == 3:&nbsp; &nbsp; numberSuffix = "rd of all"else:&nbsp; &nbsp; numberSuffix = "th of all"&nbsp; &nbsp;&nbsp;

Helenr

只需添加特殊情况:def get_ending(num):&nbsp; &nbsp; if num in [11, 12, 13]:&nbsp; &nbsp; &nbsp; &nbsp; return "th"&nbsp; &nbsp; lastdig = num % 10&nbsp; &nbsp; if lastdig == 1:&nbsp; &nbsp; &nbsp; &nbsp; return "st"&nbsp; &nbsp; elif lastdig == 2:&nbsp; &nbsp; &nbsp; &nbsp; return "nd"&nbsp; &nbsp; elif lastdig == 3:&nbsp; &nbsp; &nbsp; &nbsp; return "rd"&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; return "th"# more code...while True:&nbsp; &nbsp; number += 1&nbsp; &nbsp; numberSuffix = get_ending(number) + " of all"&nbsp; &nbsp; print(str(number) + numberSuffix)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python