Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (45.3k 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
asked Feb 3, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Oct 11, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...