我不明白为什么这是一个“语法错误”?

huangapple go评论82阅读模式
英文:

I don't understand why this is a "syntax error"?

问题

我试图创建一个消息列表,这些消息将被随机选择。我一直在收到语法错误,我正在努力找出我漏掉了什么?

我已经查看了代码,但看不到任何问题,但我觉得有些我不知道的东西,我尝试了在线检查器,但它们只会显示错误而不告诉我错误是什么...

messageRnd = ['倒计时!' + str(days_left) + '天', '天哪!还有' + str(dayt_left) + '天要到了!!', '只是个提醒,还有' + str(days_left) + '天要到了!', '还剩下' + str(days_left) + '天!', 'T-' + str(days_left) + ' 起飞。', '你知道吗,还有' + str(days_left) + '天才起飞?']
英文:

I am trying to make a list of messages that will be randomly chosen from. I keep getting syntax error, I'm trying to figure out what I've missed?

I have looked over the code and can't see anything, but I feel like there's something I don't know about, I tried online checkers, but they just say errror and don't show me what the error is ...

messageRnd = ['Countdown time!'(days_left)'','OMG! '(dayt_left)'days to go!!', 'Just a reminder, there are'(days_left)' days to go!', ' '(days_left)'days left!', 'T-'(days_left)' to takeoff.', 'did you know there are '(days_left) 'days until we takeoff?']

答案1

得分: 1

from random import randint
while True:
    days_left = str(randint(0, 100))
    messageRnd = [f'Countdown time! {days_left}',
                  f'OMG! {days_left} days to go!!',
                  f'Just a reminder, there are {days_left} days to go!',
                  f'{days_left} days left!',
                  f'T-{days_left} to takeoff.',
                  f'Did you know there are {days_left} days until we take off?']
    print(messageRnd[randint(0, len(messageRnd) - 1)])
英文:
from random import randint 
while True:
    days_left = str(randint(0,100))
    messageRnd = ['Countdown time! ' + (days_left),
              'OMG! ' + (days_left) + ' days to go!!',
              'Just a reminder, there are ' + (days_left) + ' days to go!',
              (days_left) + ' days left!',
              'T-' + (days_left) + ' to takeoff.',
              'Did you know there are ' + (days_left) + ' days until we takeoff?']
    print(messageRnd[randint(0,len(messageRnd) - 1)].replace("days_left", days_left))

You cannot just put the variable name in, you need a , or + to concatenate. Here you need to use + as the variable would be treated as a separate entry in the list instead.

You would be far better off using an f string if you want to include many variables in a string as it is easier to type and read.

This allows you to have a more readable list, at the cost of having to redefine it if days_left changes:

from random import randint    
while True:
        days_left = str(randint(0,100))
        messageRnd = [f'Countdown time! {days_left}',
                    f'OMG! {days_left} days to go!!',
                    f'Just a reminder, there are {days_left} days to go!',
                    f'{days_left} days left!',
                    f'T-{days_left} to takeoff.',
                    f'Did you know there are {days_left} days until we take off?']
        print(messageRnd[randint(0,len(messageRnd) - 1)])

As Cmd858 said in the comments, you could also do the following to avoid having to redefine the list while having the readability advantage:

from random import randint 
messageRnd = ['Countdown time! days_left',
            'OMG! days_left days to go!!',
            'Just a reminder, there are days_left days to go!',
            'days_left days left!',
            'T-days_left to takeoff.',
            'Did you know there are days_left days until we take off?']
while True:
    days_left = str(randint(0,100))
    print(messageRnd[randint(0,len(messageRnd) - 1)].replace("days_left", days_left))

huangapple
  • 本文由 发表于 2023年7月7日 00:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定