Python Forum
Syntax error on line 29 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Syntax error on line 29 (/thread-32700.html)



Syntax error on line 29 - manwithjeans - Feb-27-2021

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)        



RE: Syntax error on line 29 - deanhystad - Feb-27-2021

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'



RE: Syntax error on line 29 - manwithjeans - Feb-27-2021

(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


RE: Syntax error on line 29 - perfringo - Feb-27-2021

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.