-4

I am using Python 3.6.5, and what I want is: when my variable go is set to 1, my for i in range() loop to stop running. However, when I put for i in range(go = 1), it outputs:

TypeError: range() does not take keyword arguments

UPDATE: Here is my full range() code:

for i in range(go = 1):
  names.append(input(str(i)+": "))

UPDATE 2: Here was my code before:

for i in range(amount):
  names.append(input(str(i)+": "))

In this, amount is equal to whatever the user inputs, meaning however many names they want to fill.

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
  • 1
    try `range(3, 10, 2)` or `range(3, 10)` or `range(10)` to see what it does – fafl Oct 14 '18 at 18:53
  • Can you please post your full `for` loop. Perhaps you can just use a `break` statement inside the loop and check what `go` is set to there. – slider Oct 14 '18 at 18:54
  • 1
    Possible duplicate of [How to use a decimal range() step value?](https://stackoverflow.com/questions/477486/how-to-use-a-decimal-range-step-value) – fafl Oct 14 '18 at 18:54
  • @fafl I know what it does; I just want it to accept keyword arguments. –  Oct 14 '18 at 18:54
  • @ArihanSharma then you will have to write your own function, `range` does not accept keyword arguments – fafl Oct 14 '18 at 18:55
  • sounds like a while loop – vash_the_stampede Oct 14 '18 at 18:55
  • 1
    What would this code even do? What would `i` be in each iteration? – Daniel Roseman Oct 14 '18 at 18:57
  • Please look at my *revised* edit. –  Oct 14 '18 at 19:06
  • 1
    Which doesn't have any reference to `go`. So things are even less clear than before. What is `go`, why do you want to pass it to `range`, and what was wrong with the code you had? – Daniel Roseman Oct 14 '18 at 19:08
  • @DanielRoseman This is my code before I even had `go`; basically `go` determines when the user clicks my button, called `randomize`. `go`indicates that 'the user has clicked the button, stop asking user input anymore and randomize all of the names'. –  Oct 14 '18 at 19:12
  • What is this button? Is this a web app or using some GUI framework? How will the code know that `go` has been clicked? This sounds *exactly* like a while loop. – Daniel Roseman Oct 14 '18 at 19:15

2 Answers2

3

I think you want a while instead of a for ... in

example:

while go != 1:
    ...
Ken Kinder
  • 12,654
  • 6
  • 50
  • 70
  • But then how will I break out? It will keep on running that and never get to the part where `go = 1`. –  Oct 14 '18 at 19:02
  • @ArihanSharma It will break when you set go to 1 inside the `while`. ex: `go=0; while go != 1: go = int(input('inform go value: '))` – Victor Duarte da Silva Oct 14 '18 at 19:16
0

As I mentioned this sounds like a while loop, here is the basic concept you can test for yourself

go = 0
while go != 1:
    print('test')
    go = 1
print('test complete')
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20