I dont quite understand the while loop in python

2024/10/15 16:23:06
def AddSingleCard(self):symbols = ['heart', 'diamond', 'club', 'spade']#newCardSign = ''newCardNumber, newCardSign = raw_input().split()try:newCardNumber = int(float(newCardNumber))except:newCardNumber, newCardSign = raw_input().split()while (newCardNumber not in (2,15) or newCardSign not in symbols):newCardNumber, newCardSign = raw_input().split()newCard = [newCardNumber, newCardSign]

I'm trying to loop until the input will be a number between 2-15, and the string will be one of the symbols, but the while loop works for me only if the wrong input is the numbers, if the numbers are in range and the string is not, the program just gets stuck in the while line, and waits for next input, instead of going down to the next line and get the input to the right place... I believe it's connected to the syntax of my while loop, but I can't put my finger on the problem. (my programming background is c, I'm new to python)

tnx!

Answer

If you know C, you might want to use a do ... while loop, because the loop body must be executed at least once. But there is no such thing as a do ... while loop in Python. You have to start with while True: and break out of the loop if your condition is met.

def AddSingleCard(self):symbols = ['heart', 'diamond', 'club', 'spade']while True: newCardNumber, newCardSign = raw_input("Enter card number and sign (heart, diamond, club, spade), seperated by space").split()try:newCardNumber = int(newCardNumber)except ValueError:print "Card number must be a number between 2 and 15"continueif newCardNumber in range(2,16) and newCardSign in symbols:breakprint "Card number or symbol not valid"newCard = [newCardNumber, newCardSign]
https://en.xdnf.cn/q/117814.html

Related Q&A

Adding a FileField to a custom SignupForm with django-allauth

I have the following custom SignupForm (simplified, works perfectly without my_file):class SignupForm(forms.Form):home_phone = forms.CharField(validators=[phone_regex], max_length=15)my_file = forms.Fi…

Checkbox to determine if an action is completed or not

I have a list of dictionaries of clients in a format like this:dict_list = [{Name of Business : Amazon, Contact Name : Jeff Bezos, Email : [email protected]}, {Name of Business : Microsoft, Contact Nam…

Python not concatenating string and unicode to link

When I append a Unicode string to the end of str, I can not click on the URL.Bad:base_url = https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&tit…

Decrypt python/django password with Symfony 2.5 (using symfony security)

I want to use symfony 2.5.10 security in order to login in from users that were created with pyhton/django security. Passwords in db that are encrypted in this format:pbkdf2_sha256$12000$dVPTWPll8poG$3…

Reuse getCmd object in pysnmp

In the pysnmp documentation there is a getCmd class, I was wondering if it was possible to just instantiate the class once and reuse it at a later point by passing it new oids. I am not sure if the ge…

Django plugged into Apache not working like Django standalone

I have encountered a hodgepodge of errors trying to bring my django site into production with Apache. Having finally gotten mod_wsgi sorted and Apache at least seeming to be trying to load the site Im …

How to filter overlap rows in a big file in python

I am trying to filter overlap rows in a big file in python.The overlap degrees is set to 25%. In other words,the number of element of intersection between any two rows is less than 0.25 times of union …

Installing wxPython in Ubuntu 12.10

I am trying to install wxPython on my Ubuntu 12.10 but with no success. I have gone through all the answers given on this website. Can someone please help me in this or point me in the right direction.…

Open a file name +date as csv in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

TextCtrl providing an out of bound exception in wxPython

I am new to WX, so I decided to make a program that will periodically write out a line of text to the screen based on an outside input. The basis of the program contains a basic window with the multili…