![]() |
|
Python - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58) +--- Thread: Python (/showthread.php?tid=39965) |
Python - Manu - 2011-03-30 Sup, I'm beginning to learn python, only know the very very very very basics so far, and I'm creating this thread so I (and other people if they need) can ask for help on things they can't do. I'm kind of stuck on this simple and probably 'python 101' problem, it's to make a binary number a regular one. What I wrote (ignoring my spanish variable use) is Code: suma=0According to me this asks for the binary code, and then should transform it, but something I can't find is wrong with it and I always get 0 as result, I've tried changing some things but I can't get it right. My first exam is on saturday so I'll probably ask more stuff about basic coding like this. Python - Fiel - 2011-03-30 Sería mejor hacerlo así: Code: numero = raw_input("Ingresa codigo binario: ")I have to look at your code to see what's wrong. There's something weird with the range() call. Ah, I know what the problem is. If your input were "1000", your range would result to "range(-1, -5)" The range() function uses range(start, stop, step) Because the start is greater than the stop, the loop never happens. You need to include a step number. "range(-1, -5, -1)". This way your loop will go backwards until it reaches -5. Otherwise your start is greater than your stop, so the loop never runs. Python - Manu - 2011-03-30 I found out where my mistake was, I misread the way on how to transform from binary to decimal, I rememberd how to but guided myself from the example and screwed it up making it backwards. The code that gets it right is: Code: final=0Edit: Also, looking and running your code, you use functions I haven't really learned yet xD I hadn't seen enumerate yet, or the way you reversed the number, we do it more... stupidly because we're noobies at my class xD, also we've never used more than one variable in a 'for x in range', when we need more than one we do: Code: for x in range (y,z):Python - Fiel - 2011-03-30 It's syntactic sugar. Code: for i, eachVariable in enumerate(variable):^^^^ is the same as ^^^^ Code: i = 0Python - Manu - 2011-03-30 let me see if I got it right example: for i, eachvariable in enumerate(124612) print i+2, will result in: 3,4,6,8,3,4? and also, x+= something is the same as x= x+something? Python - Fiel - 2011-03-30 You can't enumerate over a number. You can enumerate over strings, arrays, and dictionaries as they are iterable. Code: Variable = "124612"Code: 2"x +=" is the same as "x = x +" Python - Manu - 2011-03-30 Fiel Wrote:You can't enumerate over a number. You can enumerate over strings, arrays, and dictionaries as they are iterable.oh, ok. Thanks for the help
Python - Manu - 2011-03-31 Ok I have a harder one now, I have to get the sine function, based on this equation: ![]() the input variables are x and p, p being precision, where the difference between the abs of 2 consecutive values has to be lower than p for the thing to be precise. Once again, ignore my annotations as they are in spanish, and there's a lot of them:#neutros y contadores
Spoiler
the example given to us is:x: 0.5236 p: 0.01 output:0.500003192986 When I try it out, it looks like there's a problem with my 'frac' that goes to 0 and reaches an endless loop. Any ideas on how to make it work? Python - Fiel - 2011-04-01 The problem with your loop is that in "while n>p" you reassign neither N nor P, so you never break out of the loop. Code: #Calculate sen(x)Or, if you can use imports Code: [b]import math[/b]Python - Manu - 2011-04-01 Thanks, that was very helpful ![]() unfortunately we can't import things since we haven't really 'learned' how to use it, on the test we can only use things that we've been taught. I have another problem now, it's about lotka-volterra's equations. if x is rabbits and y are wolves and the eco system variables are a=3e-2 b=4e-5 c=5e-2 d=6e-6 I have to follow the equation for how it decreases everyday until rabbits are extinct: x decrease = x(a-by) y decrease = -y(c-dx) if there's 300 rabbits and 400 wolves then how many days does it take for it to be rabbits<1 I have: Code: a=3e-2and the correct code is: Code: a = 3e-2Edit: I found out what was wrong, apparently the condition for some reason is that wolves<1 which in my head makes no sense since it's rabbits that I'm waiting to be extinct.... my code is fine except that I thought the condition instead of just copying what was there Python - Fiel - 2011-04-01 Misplaced negative sign Code: while con > 1: |