Posting Freak
Posts: 2,354
Threads: 88
Joined: 2008-07
Gender: Male
Country Flag: chile
IGN: ManuFistsYou
Server: LoL
Job: Awesome
Guild: Demacia
Farm: Trenzalore
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=0
n=str(raw_input('ingresa codigo binario: '))
x=len(n)-1
for i in range (-1,-(len(n))):
q=(n[i])
suma=suma+(int(q))*(2**x)
print suma
x=x-1
print suma
According 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.
Test Mushroom
Posts: 10,045
Threads: 1,506
Joined: 2008-06
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: GuavaCowboy
Server: Zenith
Level: 10x
Job: Jett
Guild: L>
Sería mejor hacerlo así:
Code: numero = raw_input("Ingresa codigo binario: ")
numero = numero[::-1] #Ponlo en reverse
resulta = 0
for i, cadaDigito in enumerate(numero):
if cadaDigito == '1':
resulta += 1 << i
print(resulta)
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.
Posting Freak
Posts: 2,354
Threads: 88
Joined: 2008-07
Gender: Male
Country Flag: chile
IGN: ManuFistsYou
Server: LoL
Job: Awesome
Guild: Demacia
Farm: Trenzalore
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=0
n=raw_input('ingresa codigo binario: ')
L=len(n)
x=len(n)-1
for i in range (-L,0):
p=n[i]
final=final+(int(p))*(2**x)
x=x-1
print 'total=',final
... Thanks for the help, I'll probably ask more things later/this week.. or even after that
Edit: 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):
for j in range(p,q):
something something
but as I said, still learning.... slowly... xD
Test Mushroom
Posts: 10,045
Threads: 1,506
Joined: 2008-06
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: GuavaCowboy
Server: Zenith
Level: 10x
Job: Jett
Guild: L>
It's syntactic sugar.
Code: for i, eachVariable in enumerate(variable):
pass
^^^^ is the same as ^^^^
Code: i = 0
for eachVariable in Variable:
#Do stuff
i += 1
Posting Freak
Posts: 2,354
Threads: 88
Joined: 2008-07
Gender: Male
Country Flag: chile
IGN: ManuFistsYou
Server: LoL
Job: Awesome
Guild: Demacia
Farm: Trenzalore
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?
Test Mushroom
Posts: 10,045
Threads: 1,506
Joined: 2008-06
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: GuavaCowboy
Server: Zenith
Level: 10x
Job: Jett
Guild: L>
You can't enumerate over a number. You can enumerate over strings, arrays, and dictionaries as they are iterable.
Code: Variable = "124612"
for i, eachVariable in enumerate(Variable):
print(i + 2)
"x +=" is the same as "x = x +"
Posting Freak
Posts: 2,354
Threads: 88
Joined: 2008-07
Gender: Male
Country Flag: chile
IGN: ManuFistsYou
Server: LoL
Job: Awesome
Guild: Demacia
Farm: Trenzalore
Fiel Wrote:You can't enumerate over a number. You can enumerate over strings, arrays, and dictionaries as they are iterable.
Code: Variable = "124612"
for i, eachVariable in enumerate(Variable):
print(i + 2)
"x +=" is the same as "x = x +" oh, ok.
Thanks for the help
Posting Freak
Posts: 2,354
Threads: 88
Joined: 2008-07
Gender: Male
Country Flag: chile
IGN: ManuFistsYou
Server: LoL
Job: Awesome
Guild: Demacia
Farm: Trenzalore
Ok I have a harder one now, I have to get the sine function, based on this equation:
![[Image: seno.png]](http://localhostr.com/file/zDUCRQj/seno.png)
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
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?
Test Mushroom
Posts: 10,045
Threads: 1,506
Joined: 2008-06
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: GuavaCowboy
Server: Zenith
Level: 10x
Job: Jett
Guild: L>
2011-04-01, 03:12 AM
(This post was last modified: 2011-04-01, 05:29 PM by Fiel.)
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)
inputValue = 0.5236
precision = 0.01
loopCounter = 1
finalValue = 0
mathSymbol = 'add'
while True:
#Create numerator
numerator = (inputValue**loopCounter)
#Create denominator
denominator = 1
for i in range(1, loopCounter+1):
denominator *= i
#Perform division
fractionResult = float(numerator) / float(denominator)
loopCounter += 2
#Save the previous value to affirm precision
prevValue = finalValue
#Perform addition or subtraction
if mathSymbol == 'subtract':
finalValue -= fractionResult
mathSymbol = 'add'
elif mathSymbol == 'add':
finalValue += fractionResult
mathSymbol = 'subtract'
#Affirm precision
if loopCounter > 3:
if precision > abs(prevValue) - abs(finalValue):
print("OUTPUT: %0.12f" % finalValue)
break
Or, if you can use imports
Code: [b]import math[/b]
#Calculate sen(x)
inputValue = 0.5236
precision = 0.01
loopCounter = 1
finalValue = 0
mathSymbol = 'add'
while True:
#Create numerator
numerator = (inputValue**loopCounter)
[b]#Create denominator
denominator = math.factorial(loopCounter) [/b]
#Perform division
fractionResult = float(numerator) / float(denominator)
loopCounter += 2
#Save the previous value to affirm precision
prevValue = finalValue
#Perform addition or subtraction
if mathSymbol == 'subtract':
finalValue -= fractionResult
mathSymbol = 'add'
elif mathSymbol == 'add':
finalValue += fractionResult
mathSymbol = 'subtract'
#Affirm precision
if loopCounter > 3:
if precision > abs(prevValue) - abs(finalValue):
print("OUTPUT: %0.12f" % finalValue)
break
Posting Freak
Posts: 2,354
Threads: 88
Joined: 2008-07
Gender: Male
Country Flag: chile
IGN: ManuFistsYou
Server: LoL
Job: Awesome
Guild: Demacia
Farm: Trenzalore
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-2
b=4e-5
c=5e-2
d=6e-6
con=300.0
lob=400.0
t=0
while con > 1:
p=con*(a-(b*lob))
q=-lob*(c-(d*con))
con=con-p
lob=lob-q
t=t+1
print 'con=',con
print 't=',t
print 'Los conejos se extinguiran en '+str(t)+' dias'
and the correct code is:
Code: a = 3e-2
b = 4e-5
c = 5e-2
d = 6e-6
x = 300.0
y = 400.0
dias = 0
while y>1 :
xi = x
x = x + x*(a - b*y)
y = y - y*(c - d*xi)
dias += 1
print 'Los conejos se extinguen en', dias
I just can't see what's wrong with mine.
Edit: 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
Test Mushroom
Posts: 10,045
Threads: 1,506
Joined: 2008-06
Gender: Male
Sexual Orientation: Straight
Country Flag: usa
IGN: GuavaCowboy
Server: Zenith
Level: 10x
Job: Jett
Guild: L>
Misplaced negative sign
Code: while con > 1:
p=con*(a-(b*lob))
q=-lob*(c-(d*con))
con=con-p
lob=lob-q #Aqui no se debe restar - agregalos pq ya habias incluido el "-" antes con "q=". Un doble negativo = positivo
t=t+1
print 'con=',con
print 't=',t
|