gametechmods

Off-Topic => Off-Topic Discussion => Topic started by: Badger on January 28, 2013, 02:24:00 PM

Title: The "I Need Help With Code" Thread
Post by: Badger on January 28, 2013, 02:24:00 PM
Because I don't want to make a new thread every time I mess up in a new programming language.

So I'm learning VBscript and I'm getting a compile error every time because apparently there's a loop without a do.

Code: [Select]
Dim no

no = 10

Set obj = CreateObject("Scripting.FileSystemObject")

Do while no = 10
    If obj.FolderExists("..\world") Then
    obj.CopyFile "..\world", "..\Backups"
    WScript.sleep 86400000 '1 day in milliseconds, according to google.
Loop while no = 10
But, as you can see, there is a Do (Line 7). So, how do I fix this?
Title: Re: The "I Need Help With Code" Thread
Post by: Pwnator on January 28, 2013, 05:17:20 PM
Don't really know anything much about VB (other than he is actually Scrap), but have you tried placing parentheses on the conditions?
Title: Re: The "I Need Help With Code" Thread
Post by: Badger on February 01, 2013, 02:33:37 PM
The If statement in the loop didn't have and 'End If'. Stupid mistake, strange error message.

I have a new problem now, the script won't detect the world/autosave folders. The code and folder is below.

(https://gametechmods.com/uploads/images/64643tekkitfiles.png)

Code: [Select]
Set obj = CreateObject("Scripting.FileSystemObject")

Dim worldfolder
Dim backupfolder

worldfolder = "..\world"
backupfolder = "..\autosave"

Do
    If (obj.FolderExists("..\world")) Then
    msgbox "World file found!"
    obj.CopyFolder "..\world", "..\autosave"
    obj.CopyFile "..\world\*", "..\autosave"
    msgbox "Autosave complete!"
    Else
    msgbox "Error"
    Wscript.quit
    End If
        WScript.sleep 21600000 '6 hours in milliseconds, according to google.
Loop
Title: Re: The "I Need Help With Code" Thread
Post by: Badger on July 14, 2013, 06:44:26 AM
Alright, so now in Python 3.3 (don't ask why) I keep getting a syntax error with the following code:

Code: [Select]
import math
print("A^2 x 2B x C")
numa = input('What is A? ')
numb = input('What is B? ')
numc = input('What is C? ')
firstanswer = (numb + math.sqrt((math.pow(numb,2)+4*numa*numc))/(2*numa)
secondanswer = (numb - math.sqrt((math.pow(numb,2)+4*numa*numc))/(2*numa)
print("The answer is")
print(firstanswer)
print("or")
print(secondanswer)

And when I try to run it, I get the following error:

Code: [Select]
    secondanswer = (numb - math.sqrt((math.pow(numb,2)+4*numa*numc))/(2*a)
               ^
SyntaxError: invalid syntax

What am I doing wrong? Why is this only happening with the second variable? Why doesn't it run the program until it encounters the error, rather than just flagging it up? =S
Title: Re: The "I Need Help With Code" Thread
Post by: Pwnator on July 14, 2013, 06:51:53 AM
Code: [Select]
    secondanswer = .../(2*a)
Looks like that's the problem. I'm gonna guess 'a' should be 'numa'.

hooray for quadratic equations


[Edit] Ah, crap. Forgot bold or italic doesn't work within codes. :<
Title: Re: The "I Need Help With Code" Thread
Post by: Badger on July 14, 2013, 06:58:04 AM
Thanks, I can't believe I made such a stupid mistake. I wish variables worked properly in Python. This weakly typed rubbish is annoying. Oh, and if I'm starting with a programming language, I always make a quadratic calculator as soon as I learn the math. May as well make something useful out of it ASAP.

Edit:


Nope, same error. It's very odd how the 1st variable doesn't give an error but the near identical 2nd one does.
Title: Re: The "I Need Help With Code" Thread
Post by: noogai03 on July 14, 2013, 12:02:45 PM
Code: [Select]
import math
import __future__  #SORRY I GOT THIS WRONG FIRST TIME

print "A*x^2+B*x+C"
a = input()
b = input()
c = input()


#make sure no invalid stuff is happening, to prevent division
#by 0, sqrt of minus nos
if b ** 2 - 4*a*c < 0 or 2*a ==0:
    print "error"
    exit()


ans1 = (  -b + math.sqrt(  (b ** 2)-(4*a*c)  )) / (2*a)
ans2 = (  -b - math.sqrt(  (b ** 2)-(4*a*c)  )) / (2*a)

print ans1
print ans2

My version, this should work fine
Title: Re: The "I Need Help With Code" Thread
Post by: noogai03 on July 14, 2013, 12:08:23 PM
Code: [Select]
import math
print("A^2 x 2B x C")
numa = input('What is A? ')
numb = input('What is B? ')
numc = input('What is C? ')
firstanswer = (-numb + math.sqrt(numb**2 - 4*numa*numc))/(2*numa)
secondanswer = (-numb - math.sqrt(numb**2 - 4*numa*numc))/(2*numa)
print("The answer is")
print(firstanswer)
print("or")
print(secondanswer)
Fixed your version - the formula was wrong and I think math.pow() broke it
Title: Re: The "I Need Help With Code" Thread
Post by: Badger on July 14, 2013, 12:15:14 PM
Fixed. Brackets, man. FML. Thanks to all who responded.
Title: Re: The "I Need Help With Code" Thread
Post by: Badger on August 27, 2013, 06:52:47 PM

Alright, managed to pinpoint the problem

Code: [Select]
numbertocheck = numberslist[x]I want to take a number from numberslist with index number 'x', and compare it to this variable:
Code: [Select]
    pivot = numberslist[pivotindex]
However, the variable 'pivot' is an integer (as expected). However, python is treating 'numbertocheck' as a list, making it incomparable to pivot! Why is this, and how can I fix it?