0
votes
4answers
57 views

For Loop vs While Loop differences PYTHON

I am a beginner to Python, and my professor does poor job explaining the differences between loops. I wanted to ask this community the differences between For loops and While loops. I looked at ...
0
votes
1answer
38 views

Problems using random.choice in a while loop

I'm trying to write a program that models the Prisoner's Dilemma (for my own research purposes), and I'm having trouble generating a random computer choice for every round of the game. Here is my ...
0
votes
3answers
82 views

Why does my simple program never exit my while loop?

It's a simple number guessing game, and the code is as follows: from random import randint number = randint(0,20) playerGuess = '0' def guess(): playerGuess = input("Guess a number: ") if ...
-2
votes
3answers
46 views

Python: How to end while True loop

How would I go about ending this while True loop, I want the loop to end at the marked location, but i tried both break and continue, both don't end the loop. It just keeps the script running and it ...
1
vote
1answer
36 views

Python: model bacterial growth, nested while loop, logical error

I have to model bacterial growth as a function of optical density. The problem is simple: for every hour specified by the user, the OD in each cell in a table increases by 5%; when the cell reaches an ...
1
vote
1answer
29 views

Python indentation error while loop

while x <= 9: result = usertype() if result == 'Correct': I am receiving an indentation error on the "t" of result in "result = usertype(). Can anybody explain this? *EDIT I have tried ...
-1
votes
0answers
52 views

Python game loops continues repeating

So I'm creating a game(actually an experiment) and the problem is that the while (current time-start time) < 15 min: loop is just repeating the body of the code, and I need it to go through once, ...
-2
votes
1answer
41 views

How to run a while loop in the background of the same program in Python 2.7?

I am trying to create a reaction game that will penalize users for reacting too quickly. I need to be able to run the program and while the program is running, checking for users that input too ...
0
votes
2answers
26 views

Python loop to run for certain amount of seconds

I have a while loop, and I want it to keep running through for 15 minutes. it is currently: while True: #blah blah blah (this runs through, and then restarts. I need it to continue doing this ...
1
vote
1answer
25 views

While Loop Print List formatting

I wanted to create a list of factors from any given number only using the formula below. I am not allowed to use list therefore, I have imitate using strings as follows: for example and lets say we ...
-1
votes
1answer
29 views

Python list indices json error

I really have no idea what's going on here -- let me outline the code. I'm pulling data from the SFG WorldCup API. Sample output from the API from earlier today: [ { "match_number": 26, ...
0
votes
2answers
40 views

list/deque and nested for-loop

I'm new to Python and am running into a bit of a road block. I'm using python 3 and I have the following code: from collections import deque databases=input("Enter databases: ") db_list = ...
0
votes
3answers
77 views

Need another set of eyes on my loops

Starting with a list of coordinates, I'm trying to create a new list with interpolated coordinates included. I'm missing something and it just appends the first and last coordinate over and over ...
-3
votes
3answers
47 views

python while loop quit variable

I am trying to figure out how to do quit variable in a while loop. I am learning python, so this is new. Should I start with variable1 variable2 print("Press enter to continue, or type "quit" ...
1
vote
1answer
36 views

while loop - IndexError: list index out of range

Maybe it's too simple and I just didn't see my mistake. while list_a[y] in list_a != list_a[-1]: print(y);y=y+1 returns IndexError: list index out of range list_a looks like: ['00001', ...
1
vote
3answers
35 views

How can I make raw_input tolerance more robust in Python 2.7?

I'm new here and to programming so please take it easy. I understand that almost anybody reading this knows more than I do and that I'm probably doing it wrong. That being said, this is what I'm ...
0
votes
1answer
41 views

While loop comparison behaving odd [duplicate]

I am parsing a list of data from a file I am reading in, and every line ends with a '\n' character. When I try to stop at the end of the line, my while does not stop at the newline character. while ...
0
votes
1answer
23 views

How to add a Collision in Pygame

i'm making a game about a car trying not to collide with pedestrian car. I'm trying to add a collision to the user_car(aka Player class) with enemy(aka pedestrian_cars class), but i'm not exactly sure ...
0
votes
3answers
60 views

Replace simple while loop by recursion

If I have an simple function (pseudocode) like fun(a, b): x = a i = 0 while i < b do x = x + a i ++ return x Can I replace the while loop in this function by a recursion?
-2
votes
1answer
68 views

Restart a while loop? [closed]

i need help with a password program im creating: while len(pw) <= 6 and len(pw) >= 12 is True: print ('') else: print ('Please Re-Enter your password, as it must be between 6-12 ...
0
votes
3answers
74 views

Loop with numbers in rows of the csv file

I wrote a python script to run a sql in PostgreSQL, import sys, os, math os.chdir(r'C:\Users\Heinz\Desktop') print os.getcwd() #set up psycopg2 environment import psycopg2 #shortest_path module ...
-3
votes
3answers
54 views

Recursion and while loop for Python

def rec_naive(a,b): while a>0: b + rec_naive(a,b) a=a-1 return b + rec_naive(a,b) print rec_naive(5,2) I'm not sure what I am doing wrong, but I keep on getting an error ...
0
votes
1answer
35 views

tkinter root.mainloop with While True loop

I am using tkinter to display some labels based on voltages I am reading. However, it stops executing after one read. I have found that it is due to root.mainloop(). But I am not able to fix it. I ...
0
votes
3answers
46 views

while loop and skipping numbers in python

I am making a while loop counter that i want to skip a certain numbers and those number's value+10, +20, +30...and so on. Here's what i have been trying so far: lstLength = 26 skipNum = [2, 8] lst = ...
0
votes
3answers
36 views

Check user input is valid for decimal?

I want to make sure the input will be number. I have tried testing with signs and letters but the shell just throws an error saying "Invalid literal for Decimal". I'm working on a calculator so ...
0
votes
3answers
67 views

Basic while loops in Python

I have been trying to make a simple game in Python 3.3.4, at the beginning of the game I want the user to select a difficulty between 1 and 3 and if they put in another character other than 1, 2 or 3 ...
-7
votes
3answers
45 views

how to break out of this while loop [closed]

guys i am trying to break out of this while loop.. starterP=input("Would you rather Torchik, Mudkip, or Bulbasaur? Choose wisely.") if starterP=='Torchik' or starterP=='torchik': print("You ...
-4
votes
1answer
57 views

How to stop a infinite while loop

I am writing this simple game for an assignment in my class. I am trying to make it so that once the user enters “yes”, it stops asking them if they would like to proceed to the Kanto region. But if ...
0
votes
1answer
70 views

Why does this while loop “hang”?

I have this while loop in a program I am writing. It acquires transaction data from a website and generates a response. It runs as long as there are no transactions from the site, but when it ...
1
vote
2answers
51 views

While loop from How to think like computer scientist

I am complete newbie and just have started with Think Python book. On the chapter about Iteration the author brings examples about while loop as following: def countdown(n): while n > 0: ...
1
vote
3answers
58 views

Python while loop. What is this construct called?

On the Literate Programs site, I was looking at the Python code for the GCD algorithm. def gcd(a,b): """ the euclidean algorithm """ while a: a, b = b%a, a ...
0
votes
4answers
78 views

While-Loop Use in Python

When you come to the 2nd while loop while x == 2: it's still repeating the whole script even though x /= 1 (not if "n"is entered). Let say we enter "y" on the prompt "Is this correct?" shouldn't x ...
0
votes
3answers
40 views

Learn Python the Hard Way exercise 36 while loop [closed]

The while loop in the dragon_room is not running and I'm not sure why. I get the '>>' prompt over and over again and the program never exits or brings me to another room. from sys import exit def ...
0
votes
1answer
47 views

Counting frames in a video using OpenCV… (Python)

I am using a loop to access all frames of a video file using Python and OpenCV. While accessing each frame, I add the index of the frame in a list. However I compare the size of the list and the ...
0
votes
1answer
29 views

I cant run a while loop in python due to a syntax error [closed]

Can't solve a syntax error in this simple program. I don't see why after looking at many tutorials that it won't run. def wtf_whileloop(): return int(5) x=wtf_whileloop() while x=5: print ...
0
votes
0answers
19 views

How to count down and give user input option in Python? [duplicate]

Hi I have been playing around with the while loop idea to try to re-create a situation you see in games where you have a countdown before a game ends. Th player has the option to continue or end the ...
0
votes
2answers
81 views

while true loop in python can't be stopped [closed]

I have a problem with this loop in python: i = 1 while True: with open('/tmp/file.txt', 'r+') as f: for line in f: work = 'word1' + line + 'word2' + line + 'counter=' + str(i) ...
-1
votes
1answer
51 views

combining two while loops of python into one

i need to combine my python codes which when the door is closed the PIR sensor will start to sense movement. when i combine these two files, it will run only the first while loop. it did not run to ...
0
votes
1answer
51 views

Designing an RPG for class, stuck with my loops

So first of all apologies for bad code style, this is my first big project(a final for my python class). I'm stuck on the loop setup I need to move from the "main menu" displaymenu function to the ...
2
votes
0answers
73 views

Why does my function keep looping without a loop in Python?

The following code loops after line 30. Can anyone tell me what I did wrong? After typing [done] in the message, it just shows the 'title>>' prompt again. from lxml import etree import time import ...
-5
votes
1answer
36 views

Make program continue action till line has been created

Currently my program takes line1 such as "taaaaaaaaaaNataggggggggggNccc" and will cut 1 character of the end untill it matches line2 such as "taaaaaaaaaaNcccggggggggggNccc" and once they match it ...
-3
votes
2answers
37 views

while python read from file [closed]

Here is my code: variables = [0, 0, 0, 0] while (variables[2]<4): with open('vystup_online.txt') as f: for line in f: variables = (line.strip().split()) print ...
1
vote
2answers
50 views

Looking for help designing a While loop in Python

Im designing an RPG for a class project, working on a gimmicky idea of an item that can be configured to give certain bonuses to existing classes. But for some reason getting the While loop to loop ...
0
votes
2answers
70 views

How can i exit a while loop using if statements? [duplicate]

I have created a function that takes a text file of countries, puts it in a list, then asks a user for a country of residence, if the country is invalid then the program is supposed to continuously ...
0
votes
2answers
36 views

Python - sum variables from a text file

The following is what I have so far: I know I need to total the numbers in the file numberGood.txt after I change the amount to a float, but the numbers don't have variable names, ie. num1, num2 ...
0
votes
2answers
41 views

For-while-for loop

I have this kind of code. The problem is that 'kilpailija' is not changing for the other loop. Inside the third loop 'kilpailija' is always the first variable in the list numerot. How i can get it to ...
1
vote
1answer
58 views

while loop termination if end of file reached

while loop termination if end of file reached
0
votes
2answers
43 views

Replacing the values of a list whilst there is a while loop

So say I have ['habcuxabc929abc', 'tabc', 'q', 'jabcuabc9']. What I basically want to do is split each element in this list between the abc's and count the remaining parts. So the list specified ...
1
vote
1answer
50 views

How to stop a gevent micro thread when it in a while loop?

I have a problem and the situation was like code as below. The target function(wait) was in a while loop or spent a huge time, so I want to kill the micro thread. How to kill the thread1? import ...
0
votes
2answers
59 views

While Loop to produce Mathematical Sequences?

I've been asked to do the following: Using a while loop, you will write a program which will produce the following mathematical sequence: 1 * 9 + 2 = 11(you will compute this ...