I am requesting user input from a menu a script. The input I am requesting needs to be an integer. Is it possible to validate that the user actually entered an integer and not a string or just hit enter? Everything I have tried will give me
"File "<string>", line 0, in ?
NameError: name 'd' is not defined
The code I have looks like this: - def init_menu():
-
print '''
-
0) Exit
-
1) Environment
-
2) Use
-
3) OS
-
4) Arch
-
5) ALL
-
6) Done
-
'''
-
ROW = input("Choose your server type: [0]")
-
# ROW = ROW.strip()
-
print
-
# if len(ROW) == 0:
-
# print 'Exiting...'
-
# sys.exit(0)
-
if ROW == 0:
-
sys.exit(0)
-
elif ROW == 1:
-
print 'You have chosen by', INITIAL.get(ROW)
-
elif ROW == 2:
-
print 'You have chosen by', INITIAL.get(ROW)
-
elif ROW == 3:
-
print 'You have chosen by', INITIAL.get(ROW)
-
elif ROW == 4:
-
print 'You have chosen by', INITIAL.get(ROW)
-
elif ROW == 5:
-
print 'You have chosen', INITIAL.get(ROW)
-
elif ROW == 6:
-
print 'You are', INITIAL.get(ROW)
-
else:
-
sys.exit(0)
-
return ROW
The print statements for each choice are just place holders and will have actual actions associated with them eventually
Thanks,
John
4 2719
Something like this maybe, I'm just a beginner so maybe theres a better option. - row = 0
-
-
while int(row) not in range(1,8):
-
row = raw_input('''
-
0) Exit
-
1) Environment
-
2) Use
-
3) OS
-
4) Arch
-
5) ALL
-
6) Done
-
: ''')
-
-
row = int(row)
That does look better than my code but still errors if the user does not enter a number (or, more importantly for this) just hits enter.
dwblas 626
Recognized Expert Contributor
isdigit() will work fine for this. Note that it returns False for a float. - test_strs = [ "12345", " 0123", "123.45", \
-
"-123", "123x5", "12*67", " 12 45"]
-
for eachstr in test_strs :
-
print "%7s" % eachstr,
-
-
if eachstr.isdigit( ) :
-
print " IS",
-
else :
-
print " is NOT",
-
print "a digit"
Glenton 391
Recognized Expert Contributor
In this case you want to do a bunch of tests, so I would invert the logic: - while True:
-
row = raw_input('''
-
0) Exit
-
1) Environment
-
2) Use
-
3) OS
-
4) Arch
-
5) ALL
-
6) Done
-
: ''')
-
if row="": continue
-
if isdigit(row) and row in range(1,7): break
Of course the if row="": continue bit isn't necessary, but I just include it in case you need the idea for something else.
Also, the "and" operator is a short-cut operator, meaning that the second part is only evaluated when the first part is True. Handy that, or you'd get an error...
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: charleswesley |
last post by:
I find myself regularly needing to validate user input as either a)
numeric only or b) valid email address format (user@domain.tld)
I am assuming that there are regular expressions that can be used to do
this as it doesn't appear that 1.1 includes this functionality itself.
I am interested to know if anyone has a simple vb.net example of a
solution to either of these issues. I'm new to regular expressions but
if I have a working...
|
by: A TO Consultant |
last post by:
Hi All,
I am working on a web application that uses both asp classic and asp.net
pages. We need to validate user input to avoid attacks like sql injection.
Can a component be created that both page types can use? Is that the best
approach? Would I simply use pattern matching to validate strings and/or
remove any unwanted characters?
Thanks in advance.
|
by: promiscuoustx |
last post by:
Hi again all!! I am trying to write my program below to validate user input. In other words, if the user inputs something like "1a2.34", I need to return a message that states invalid data and for them to retry. I am really lost on this one, and I really need some help. Thanks so much!!
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <string>
#include <sstream>
using namespace std;
|
by: parshupooja |
last post by:
Hi,
I am working in Asp.Net 2.0, SQL Server, C#.
I am trying to create a User Input form which has lots of fields such as :
salutation;
Firstname:
lastname:
address:
Zip:
Phone:
|
by: Tamer Ibrahim |
last post by:
Hi,
I'm trying to validate user input date using range validator on a text box .
This code is throwing this exception
"The value 'DateTime.Now;' of the MaximumValue property of 'rvQDate' cannot
be converted to type 'Date'. "
<asp:TextBox ID="txtQDate" runat="server" Enabled="False"></asp:TextBox>
| |
by: Amit |
last post by:
Dear Friends
I need to write a Java Script for a string payment_code which comes
populated from a text field , should contain only 0-9,A-Z,a-z,Space '
',Hyphen '-',Full stop '.',Comma ',',Plus '+'
Characters other than these will not be allowed
If a user enters characters other than the mentioned above , the alert
message should be displayed
|
by: Rohit111111 |
last post by:
Hello all
i have created a web service which consist a web method say Add(int a, int b)
it takes two parameter a and b both are of type integer, now i have to validate the both parameter. I am using directly interface of the web service meanse i am running .asmx page now it shows 1 method Add on clicking Add link it will ask for two parameter a and b now of i enter string in that parameter instead of integer it throws an exception page. which...
|
by: ss112358 |
last post by:
Hey all!
I am making a very simple program which requires the user to enter a numerical value into a text box (html form) and then multiplies it by 10 before echoing back. The program is working fine.
However, when I enter some letters, php returns an ugly error. Is there a way to prevent the user from entering letters?
Thanks!
|
by: Tarique |
last post by:
I have tried to restrict the no. of columns in a line oriented user
input.Can anyone please point out
potential flaws in this method?
btw..
1.I have not used dynamic memory allocation because thats not something
i want to implement.
2.I suppose my input line oriented .
3.The maximum columns i need is 80
4.There is an infinite loop in the program :)
5.I have *not* tried to validate the data as yet.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |