Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error on line 29
#1
can someone please tell me how to fix this im lost here and not very good with code

import random

from random import randint

import discord

TOKEN = ''

client = discord.Client()



@client.event
async def on_message(message):
    if message.author == client.user:
        return

 
    s_nouns = ["A dude", "My mom", "The king", "Some guy", "A cat with rabies", "A sloth", "Your homie", "This cool guy my gardener met yesterday", "Superman"]
    p_nouns = ["These dudes", "Both of my moms", "All the kings of the world", "Some guys", "All of a cattery's cats", "The multitude of sloths living under your bed", "Your homies", "Like, these, like, all these people", "Supermen"]
    s_verbs = ["eats", "kicks", "gives", "treats", "meets with", "creates", "hacks", "configures", "spies on", "retards", "meows on", "flees from", "tries to automate", "explodes"]
    p_verbs = ["eat", "kick", "give", "treat", "meet with", "create", "hack", "configure", "spy on", "retard", "meow on", "flee from", "try to automate", "explode"]
    infinitives = ["to make a pie.", "for no apparent reason.", "because the sky is green.", "for a disease.", "to be able to make toast explode.", "to know more about archeology."]
 
    while true:
     messageresponse = (s_nouns[randint(0, len(s_nouns))]+" "+p_verbs[randint(0, len(p_verbs))]+" "+infinitives[randint(0, len(infinitives))]

     if message.content == 'hello':
       response=(messageresponse)
       await message.channel.send(response)

@client.event
async def on_ready():
    print('good to go')


		
client.run(TOKEN)        
Reply
#2
The error is in line 26 which has unbalanced parenthesis. About half my syntax errors are not in the line reported. Python kept looking for the missing parenthesis until the if statement forced it to give up.

A few suggestions:
Your indentation should be consistent. 4 spaces for each level is expected.

Lines should not exceed 80 characters in length. You shouldn't have to scroll sideways to see the end of a line.
s_nouns = ["A dude", "My mom", "The king", "Some guy",
           "A cat with rabies", "A sloth", "Your homie",
           "This cool guy my gardener met yesterday", "Superman"]
To randomly chose something from a list use random.choice().
choice(s_nouns) instead of s_nouns[randint(0, len(s_nouns))]
Join strings using join().
' '.join(('a', 'b', 'c')) instead of 'a' + ' ' + 'b' + ' ' + 'c'
Reply
#3
(Feb-27-2021, 04:40 AM)deanhystad Wrote: The error is in line 26 which has unbalanced parenthesis. About half my syntax errors are not in the line reported. Python kept looking for the missing parenthesis until the if statement forced it to give up.

A few suggestions:
Your indentation should be consistent. 4 spaces for each level is expected.

Lines should not exceed 80 characters in length. You shouldn't have to scroll sideways to see the end of a line.
s_nouns = ["A dude", "My mom", "The king", "Some guy",
           "A cat with rabies", "A sloth", "Your homie",
           "This cool guy my gardener met yesterday", "Superman"]
To randomly chose something from a list use random.choice().
choice(s_nouns) instead of s_nouns[randint(0, len(s_nouns))]
Join strings using join().
' '.join(('a', 'b', 'c')) instead of 'a' + ' ' + 'b' + ' ' + 'c'

i did all of that and tried to make the code better:
import random

import discord

TOKEN = ''

client = discord.Client()

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    s_nouns = ["A dude", "My mom", "The king", "Some guy",
           "A cat with rabies", "A sloth", "Your homie",
           "This cool guy my gardener met yesterday", "Superman"]
    s_verbs = ["eats", "kicks", "gives", "treats", "meets with", 
           "creates", "hacks", "configures", "spies on", 
           "retards", "meows on", "flees from", "tries to automate", "explodes"]
    infinitives = ["to make a pie.", "for no apparent reason.", 
           "because the sky is green.", "for a disease.", 
           "to be able to make toast explode.", "to know more about archeology."]
    
    messageresponse = random.choice(s_nouns, s_verbs, infinitives)

    if message.content == 'hello':
       response=(messageresponse)
       await message.channel.send(response)

@client.event
async def on_ready():
 print('we in the mainframe')
		
client.run(TOKEN)        
but now i am getting this error: Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 29, in on_message
messageresponse = random.choice(s_nouns,s_verbs,infinitives)
TypeError: choice() takes 2 positional arguments but 4 were given

i have no idea what this means
Reply
#4
Just use interactive interpreter:

+>>> import random
+>>> help(random.choice)
Help on method choice in module random:

choice(seq) method of random.Random instance
    Choose a random element from a non-empty sequence.

+>>> random.choice(range(5))
1
+>>> random.choice(range(5), range(6,9), range(7,10))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: choice() takes 2 positional arguments but 4 were given
In your original code row #30 you had true which should cause error.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a syntax error! rohes_kaugummi 3 326 Jun-23-2021, 05:24 PM
Last Post: deanhystad
  import module with syntax error Skaperen 7 357 Jun-22-2021, 10:38 AM
Last Post: Skaperen
Heart near "(": syntax error Maryan 3 483 Jun-21-2021, 08:05 AM
Last Post: ibreeden
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 631 Jun-15-2021, 08:16 AM
Last Post: Anldra12
Exclamation getting a syntax error and i don't know why ilikedofs 10 762 May-13-2021, 08:52 PM
Last Post: deanhystad
  Even number code syntax error MrCeez 1 413 May-02-2021, 06:43 PM
Last Post: Larz60+
  error in your SQL syntax Xuli 10 721 May-02-2021, 12:27 PM
Last Post: Xuli
  Function syntax error Changosoyyo 2 558 Mar-07-2021, 10:42 PM
Last Post: Changosoyyo
  Invalid syntax error - need help fixing calgk01 3 663 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Why do I get Syntax error? TechNitium 1 470 Jan-22-2021, 12:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020