0 votes
1 view
in Python by (45k points)

I'm trying to make my first Python script for a Discord Webhook, to make it short a C# script passes arguments to the Python script (the length of the arguments is variable) and need to concatenate all the arguments in one variable.

import sys 

from discord_webhook import DiscordWebhook, DiscordEmbed

argv_len = len(sys.argv)

for x in range(2, argv_len):

    message += sys.argv[x]

The error I get is: NameError: name 'message' is not defined

I expect all arguments to be saved in one variable named "message"

1 Answer

0 votes
by (16.8k points)

Simply declare the variable outside for your loop in order to access it outside the loop after working with  "for loop", as variable has a lexical scope only.

argv_len = len(sys.argv)

message = ''

for x in range(2, argv_len):

    message += sys.argv[x]

print(message)

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...