I feel like this is an easy fix but I can't figure out why I'm getting this response.
I set each if statement to have a unique error message so I can see where the error is.
Every time I run $setbirthday 05/03 in my server I get the "Invalid month specified!" while the first index of my argument (0) would seemingly pass this.
Here is the code:
class BirthdayCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.command(pass_context=true)
async def setbirthday(self, ctx, args):
user = ctx.author
role = discord.utils.get(ctx.guild.roles, name="Creator")
if role in user.roles:
if args != '':
# If it's an actual month (index 0)
if args[0] != 0 or args[0] != 1:
await ctx.send("Invalid month specified!")
else:
# If it's an actual month (index 1)
if args[1] < 0:
await ctx.send("That's not a valid month!")
elif args[1] <= 9:
# If they separated month/day using "/"
if args[2] != "/":
await ctx.send("You have to specify your birthday like `MM/DD`!")
else:
# If it's an actual day (index 0)
if args[3] > 3:
await ctx.send("Invalid day specified!")
elif args[3] < 0:
await ctx.send("That isn't a day!")
else:
# If it's an actual day (index 1)
if args[4] < 0:
await ctx.send("Incorrect day!")
else:
# Make sure there's no extra numbers
if args[5] != '':
await ctx.send("Make sure it's just `MM/DD`!")
else:
birthday = args[0] + '' + args[1] + '' + "/" + args[3] + '' + args[4]
await ctx.send("Your birthday has been registered as: " + "`" + str(birthday) + "`" + "!")
else:
await ctx.send("Error!")
else:
await ctx.send("Set your birthday by doing `$setbirthday MM/DD`")
else:
await ctx.send("You don't have the required role to set your birthday!")
There are two main problems in your code :
You should use guard clauses instead of nesting if statements, it'll make your code easier to read and understand
You can use string.split() instead of having to comment what each index corresponds to, it also simplifies your conditions.
You then obtain this code:
#commands.command()
async def setbirthday(self, ctx, arg):
user = ctx.author
role = discord.utils.get(ctx.guild.roles, name="Creator")
if role not in user.roles:
return await ctx.send("You don't have the required role to set your birthday!")
if len(arg.split('/')) != 2:
return await ctx.send("You have to specify your birthday like `MM/DD`!")
month, day = arg.split("/")
if not month.isdigit() or not 1 <= int(month) <= 12:
return await ctx.send("Invalid month specified!")
if not day.isdigit() or not 1 <= int(day) <= 31:
return await ctx.send("Invalid day specified!")
await ctx.send(f"Your birthday has been registered as: {month}/{day}!")
Few things.
Problem #1: It seems that args[0] here would be a string, namely args[0] = '05/03'. The args are split by spaces (args = the arguments passed by the discord user excluding $setbirthday, and split by space). In order to extract the first character, you would need to do args[0][0]. Which would lead us to the next problem.
Problem #2: The if args[0] != 0 or args[0] != 1: is circular in essence. The way that the or operator works is that it checks if a is true OR if b is true, then proceed into the if statement. These conditions are independent of one another. Suppose that the input is the integer (number) 1. Then before your if statement can even get to or args[0] != 1, it has already triggered if args[0] != 0. After all, 0 != 1 . The correct operator here is and.
Problem #3: As I mentioned in #1, args[0] is a string and args[0][0] is a character. Your if statement is, in essence, checking if: '0' != 0. they are indeed not the same, so the if statement would still activate. You can either convert the character in args[0][0] to an integer (if int(args[0][0]) != 0) or you can compare args[0][0] to the character '0': (if args[0][0] != '0').
To sum all of that up, try this instead:
#String/Character Comparison
if (args[0][0] != '0') and (args[0][0] != '1'):
#Int Comparison
if (int(args[0][0]) != 0) and (int(args[0][0]) != 1):
Related
I have a bot in writing in python and I want to incorporate a number game into the bot. The game code is below. (nl is a variable to say os.linesep)
secret_number = random.randint(0, 100)
guess_count = 0
guess_limit = 5
print(f'Welcome to the number guessing game! The range is 0 - 100 and you have 5 attempts to guess the correct number.')
while guess_count < guess_limit:
guess = int(input('Guess: '))
guess_count += 1
if guess > secret_number:
print('Too High.', nl, f"You have {guess_limit - guess_count} attempts remaining.")
elif guess < secret_number:
print('Too Low.', nl, f"You have {guess_limit - guess_count} attempts remaining.")
elif guess == secret_number:
print("That's correct, you won.")
break
else:
print("Sorry, you failed.")
print(f'The correct number was {secret_number}.')
So I want to be able to use that in the discord messaging system. My issue is that I need the bot to scan the most recent messages for a number from that specific user that initiated the game. How could I do so?
To simplify my question how can I say this:
if message from same message.author = int():
guess = that^
The solution here would be to wait_for another message from that user. You can do this with:
msg = await client.wait_for('message', check=message.author == PREVIOUS_SAVED_MSG_AUTHOR_AS_VARIABLE, timeout=TIMEOUT)
# You don't really need a timeout but you can add one if you want
# (then you'd need a except asyncio.TimeoutError)
if msg.content == int():
guess = msg
else:
...
I am new to Python and I have been stuck trying out a "simple banking program".
I have got everything right except for this bit:
If the user types S then:
Ask the user for the account number.
Search the array for that account number and find its position in the accountnumbers array.
Display the Name, and balance at the position found during the above search.
Originally it was just supposed to be through accounts 1-5, but now I am having trouble coming up with a way to search the account numbers if they are any number, not just 1 - 5. For example
The user makes his account numbers 34, 445, 340,2354 and 3245. Completely random account numbers with no order.
Here is what I have so far
names = []
accountNumbers = []
balance = []
def displaymenu():
print("**** MENU OPTIONS ****")
print("Type P to populate accounts")
print("Type S to search for account")
print("Type E to exit")
choiceInput()
def choiceInput():
choice = str(input("Please enter your choice: "))
if (choice == "P"):
populateAccount()
elif (choice == "S"):
accountNumb = int(input("Please enter the account number to search: "))
if (accountNumb > 0) and (accountNumb < 6):
print("Name is: " + str(names[accountNumb - 1]))
print(names[accountNumb - 1] + " account has the balance of : $" + str(balance[accountNumb -1]))
elif (accountNumb == accountNumbers):
index = names.index(accountNumb)
accountNumb = index
print(names[accountNumb - 1] + " account has the balance of : $" + str(balance[accountNumb -1]))
else:
print("The account number not found!")
elif (choice == "E"):
print("Thank you for using the program.")
print("Bye")
raise SystemExit
else:
print("Invalid choice. Please try again!")
displaymenu()
def populateAccount ():
name = 0
for name in range(5):
Names = str(input("Please enter a name: "))
names.append(Names)
account ()
name = name + 1
def account ():
accountNumber = int(input("Please enter an account number: "))
accountNumbers.append(accountNumbers)
balances()
def balances ():
balances = int(input("Please enter a balance: "))
balance.append(balances)
displaymenu()
I have tried to use indexes and have not been able to find a solution.
Replace the following line of code
if (accountNumb > 0) and (accountNumb < 6):
with
if (accountNumb > 0) and (accountNumb < len(accountNumbers)):
My mistake. I messed up when appending the account number:
def account ():
accountNumber = int(input("Please enter an account number: "))
accountNumbers.append(accountNumbers)
balances()
I appended
accountNumbers
not
accountNumber
the code should be
def account ():
accountNumber = int(input("Please enter an account number: "))
accountNumbers.append(accountNumber)
balances()
also the searchArray function I made was:
def searchArray(accountNumbers):
x = int(input("Please enter an account number to search: "))
y = accountNumbers.index(x)
print("Name is: " + str(names[y]))
print(str(names[y]) + " account has a balance of: " + str(balance[y]))
rookie mistake , shouldnt be using such similar object names.
In Maya 2015, I can get the arclen of a curve using this command:
cmds.arclen('bezier1')
But now I want to get the arclen of two points in my curve. Is there anyway to get this?
Using the Maya API, you can use the MFnNurbsCurve::findLengthFromParam (Maya 2016+ only). If you need it between two points, then call this function with each parameters and subtract.
If you don't want to use the api, then the other option is create a duplicate of your original curve and use "detach" it at the needed points and then use the arclen command on that new curve to get your length. So that is another way.
Note that when detaching a curve, it appears to try to keep the curvature as close the original as possible, but this isn't exact so the length may not be the same compared to the original curve. Maybe rebuilding the curve to have more points may increase the accuracy if that is an important factor for you.
Using Maya's API is surely the best way to do it as #scottiedoo said, but here is a function I made when I didn't know API and which gives you the same results.
from maya import cmds
def computeCrvLength(crv, startParam = None, endParam = None):
'''
Compute the length of a curve between the two given UParameters. If the both
UParameters arguments are set to None (default), will compute the length of
the whole curve.
Arguments:
- crv = string; an existing nurbCurve
- startParam = 0 <= float <= 1 or None; default = None; point parameter
value, if not None, will compute the points only between the startPt and
EndPt values.
- endParam = 0 <= float <= 1 or None; default = None; point parameter
value, if not None, will compute the points only between the startPt and
EndPt values.
Returns:
- The length of the curve between the given UParameters
- The length of the curve from its start to the startParam
- The length of the curve from its start to the endParam
'''
###### Exceptions
if cmds.objExists(crv) == False:
cmds.error ('The curve "%s" does\'nt exists.' % crv)
if cmds.filterExpand (crv, sm = 9) == None:
cmds.error ('The object "%s" is not a nurbCurve.' % crv)
if startParam != None:
if (0 <= startParam <= 1) == False:
cmds.error ('The start point parameter value must be between 0 and 1.')
if endParam != None:
if (0 <= endParam <= 1) == False:
cmds.error ('The end point parameter value must be between 0 and 1.')
if (startParam == None and endParam != None) or (startParam != None and endParam == None):
cmds.error ('The start and end points parameters must be both None or ' +
'both have values.')
if startParam != None and endParam != None:
if endParam < startParam:
cmds.error ('The end point parameter value cannot be less or ' +
'equal to start point parameter value.')
###### Function
if startParam == None and endParam == None:
crvLength = cmds.arclen (crv, ch = False)
distCrvToStartParam = 0
distCrvToEndParam = crvLength
else:
tmpArclenDim = cmds.arcLengthDimension (cmds.listRelatives(crv, s = True)[0]
+ '.u[0]')
cmds.setAttr (cmds.listRelatives(tmpArclenDim, p = True)[0] +
'.uParamValue', startParam)
distCrvToStartParam = cmds.getAttr (tmpArclenDim + '.al')
cmds.setAttr (cmds.listRelatives(tmpArclenDim, p = True)[0] +
'.uParamValue', endParam)
distCrvToEndParam = cmds.getAttr (tmpArclenDim + '.al')
cmds.delete (tmpArclenDim)
crvLength = (distCrvToEndParam - distCrvToStartParam)
return crvLength, distCrvToStartParam, distCrvToEndParam
I am trying to read one text file and convert the contents of that file to pig latin on a new file. Here is what I have:
def pl_line(word):
statement = input('enter a string: ')
words = statement.split()
for word in words:
if len(word) <= 1:
print(word + 'ay')
else:
print(word[1:] + word[0] + 'ay')
def pl_file(old_file, new_file):
old_file = input('enter the file you want to read from: ')
new_file = input('enter the file you would like to write to: ')
write_to = open(new_file, 'w')
read_from = open(old_file, 'r')
lines = read_from.readlines()
for line in lines():
line = pl_line(line.strip('\n'))
write_to.write(line + '\n')
read_from.close()
write_to.close()
However, when I run this, I get this error message:
TypeError: 'list' object is not callable
Any ideas of how to improve my code?
Here are some improvements to the actual converter:
_VOWELS = 'aeiou'
_VOWELS_Y = _VOWELS + 'y'
_SILENT_H_WORDS = "hour honest honor heir herb".split()
def igpay_atinlay(word:str, with_vowel:str='yay'):
is_title = False
if word.title() == word:
is_title = True
word = word.lower()
# Default case, strangely, is 'in-yay'
result = word + with_vowel
if not word[0] in _VOWELS and not word in _SILENT_H_WORDS:
for pos in range(1, len(word)):
if word[pos] in _VOWELS:
result = word[pos:] + word[0:pos] + 'ay'
break
if is_title:
result = result.title()
return result
def line_to_pl(line:str, with_vowel:str='yay'):
new_line = ''
start = None
for pos in range(0, len(line)):
if line[pos].isalpha() or line[pos] == "'" or line[pos] == "-":
if start is None:
start = pos
else:
if start is not None:
new_line += igpay_atinlay(line[start:pos], with_vowel=with_vowel)
start = None
new_line += line[pos]
if start is not None:
new_line += igpay_atinlay(line[start:pos], with_vowel=with_vowel)
start = None
return new_line
tests = """
Now is the time for all good men to come to the aid of their party!
Onward, Christian soldiers!
A horse! My kingdom for a horse!
Ng!
Run away!
This is it.
Help, I need somebody.
Oh, my!
Dr. Livingston, I presume?
"""
for t in tests.split("\n"):
if t:
print(t)
print(line_to_pl(t))
You very likely mixed up the assignments to read_fromand write_to, so you're unintentionally trying to read from a file opened only for write access.
Simply I need three conditions.
1) Log-in
2) Multiple request
3) Synchronous request ( sequential like 'C' )
I realized 'yield' should be used for multiple request.
But I think 'yield' works differently with 'C' and not sequential.
So I want to use request without 'yield' like below.
But crawl method wasn`t called ordinarily.
How can I call crawl method sequentially like C ?
class HotdaySpider(scrapy.Spider):
name = "hotday"
allowed_domains = ["test.com"]
login_page = "http://www.test.com"
start_urls = ["http://www.test.com"]
maxnum = 27982
runcnt = 10
def parse(self, response):
return [FormRequest.from_response(response,formname='login_form',formdata={'id': 'id', 'password': 'password'}, callback=self.after_login)]
def after_login(self, response):
global maxnum
global runcnt
i = 0
while i < runcnt :
**Request(url="http://www.test.com/view.php?idx=" + str(maxnum) + "/",callback=self.crawl)**
i = i + 1
def crawl(self, response):
global maxnum
filename = 'hotday.html'
with open(filename, 'wb') as f:
f.write(unicode(response.body.decode(response.encoding)).encode('utf-8'))
maxnum = maxnum + 1
When you return a list of requests (that's what you do when you yield many of them) Scrapy will schedule them and you can't control the order in which the responses will come.
If you want to process one response at a time and in order, you would have to return only one request in your after_login method and construct the next request in your crawl method.
def after_login(self, response):
return Request(url="http://www.test.com/view.php?idx=0/", callback=self.crawl)
def crawl(self, response):
global maxnum
global runcnt
filename = 'hotday.html'
with open(filename, 'wb') as f:
f.write(unicode(response.body.decode(response.encoding)).encode('utf-8'))
maxnum = maxnum + 1
next_page = int(re.search('\?idx=(\d*)', response.request.url).group(1)) + 1
if < runcnt:
return Request(url="http://www.test.com/view.php?idx=" + next_page + "/", callback=self.crawl)