Python Forum
Help in exercise for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help in exercise for loop
#11
well, it can be simplified to:
base = int(input("choose a power base: "))
exp = int(input("choose a power exponent: "))
result = 1
  
for _ in range(exp+1):
    result *= base
print(result)
also, it looks like you are using python2
check https://python-forum.io/Thread-Python3-2...-raw-input
and you really should be using python3
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
I'm using python2 because is the one that's using the course that I'm taking, maybe if I used python3 I wouldn't learn the lesson properly. If you tell me that it's better to use python3 anyway I will change it (as I don't understand the differences)

For the query you gave me the result is not correct as the program is changing the base for the new one in each iteration.

Thanks
Reply
#13
(Dec-01-2018, 09:46 AM)kais92 Wrote: I'm using python2 because is the one that's using the course that I'm taking, maybe if I used python3 I wouldn't learn the lesson properly. If you tell me that it's better to use python3 anyway I will change it (as I don't understand the differences)
The reason - python2 support ends 1 January 2020. python3 is the future. It is explained in the link I shared (at the bottom)

(Dec-01-2018, 09:46 AM)kais92 Wrote: For the query you gave me the result is not correct as the program is changing the base for the new one in each iteration.
I made a mistake, not to change the range you used. It should be
base = int(input("choose a power base: "))
exp = int(input("choose a power exponent: "))
result = 1
   
for _ in range(exp):
    result *= base
print(result)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
Thank, now it's correct.

I will keep with python2 as the courses I'm currently doing are with that program and I don't know if there are better courses than those.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020