Python Forum
problem with for loop using integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with for loop using integers
#1
Ok, so totally green but super keen!

I programmed a 'dice' attack value game, that prompts a user to choose n die to roll and with what 'attack value' the dice will have. the object is for a war board game, not important... Program seems to run fine, but sometimes it runs the 'for' loop more times than the user prompted for.... What am I doing wrong with this for loop? (code below)

while True: print("welcome to the dice game!") import random a=input("how many die would you like to roll? ") b=input("what is the attack value of the die? ") aa=int(a) bb=int(b) print('OK... anything ',b,'or less, is a hit!') print('Here we go!') hit=0 miss=0 for x in iter(aa): j=random.randint(1,6) if j<=bb: print(j,'hit') j=hit hit=hit+1 if j>bb: print(j,'miss') j=miss miss=miss+1 print('total hits: ',hit) print('total misses: ',miss) y=input('would you like to play again (y/n)') if y=='y': continue if y=='n': break if y!='y' and y!='n': print('invalid answer') break print('thanks for playing!')
also not sure how to paste code to include indentation on this forum... sorry.
Reply
#2
(Aug-31-2019, 02:49 AM)python_germ Wrote: Program seems to run fine,

Hmmm... I have my doubts: on line # 6 you convert aa to int and on line # 12 you create iterator from that. This would happen:

>>> aa = 5 >>> iter(aa) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thanks for your reply @perfringo.
whats the solution then? I'm trying to create a loop for a user chosen integer...
it's the wording of the phrase in line 12 I cant figure out...
Reply
#4
For starters you should follow PEP 8 -- Style Guide for Python Code .

If you want create loop from integer then use range:

>>> for i in range(3): ... print(i) ... 0 1 2
EDIT:
You don't need to use iter() in for-loop, it will be created by Python automagically (but of course it must be sequence). For more information use built-in help:

>>> help('for') # q for exiting the help
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
A bit of advise - get rid of habit to use one- or two-char cryptic variable names. It will be hell to maintain such code.
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
#6
Agree with Buran. Using descriptive variable names is helpful.

But, I think I found another issue. Inside the for loop you have two tests comparing j to bb. If the first test passes you make a change to the value of j. It is therefore possible that the comparison will pass both times, counting as both a hit and a miss. I don't think that is your intent. Instead, change line 18 to
 else:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with For loop indentation Mormolyce 10 2,985 Sep-03-2025, 03:55 AM
Last Post: DeaD_EyE
  problem program runs in a loop jasserin 0 1,318 May-18-2024, 03:07 PM
Last Post: jasserin
  While Loop Problem Benno2805 1 1,641 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Loop reading csv file problem faustineaiden 1 2,644 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  Infinite loop problem Zirconyl 5 5,350 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Dataframe mean calculation problem: do we have to loop? sparkt 1 3,383 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Reading integers from a file; the problem may be the newline characters JRWoodwardMSW 2 3,618 Jul-14-2020, 02:27 AM
Last Post: bowlofred
  Python loop problem Kristenl2784 11 8,643 Jun-18-2020, 07:22 PM
Last Post: buran
  Problem with append list in loop michaelko03 0 2,572 Feb-16-2020, 07:04 PM
Last Post: michaelko03
  problem in loop roseojha 3 3,572 Aug-26-2019, 09:03 AM
Last Post: perfringo

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.