Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How exactly do I add an additional input to this if function, or atleast add a function which will allow me to give the user another option. Thank you, in advance.
ask = input ("Would you like to 1 input an existing number plate \n or 2 view a random number? 1 or 2: ")
if ask == "1":
print("========================================================================")
ask = input()("Please enter it in such form (XX00XXX): " )
If this ask = input()("Please enter it in such form (XX00XXX): " ) is actually in your code, just changing it to ask = input("Please enter it in such form (XX00XXX): " ) will fix your problem. You had one extra pair of brackets.
DISCLAIMER: This works in Python 3.x
For Python 2, use raw_input("Your question") instead of input("Your question").
As an answer to the question in your own answer:
What is wrong in this code?
ask = input ("-Would you like to 1 input an existing number plate\n--or 2 view a random number\n1 or 2: ")
if int(ask) == 1:
print("========================================================================")
while True:
ask2 = input("Please enter it in such form (XX00XXX): " ).lower()
if re.ask3("[a-z]{2}[0-9]{2}[a-z]{3}",ask2):
print("Verification Success")
break
else:
print("Verification Failed")
You have to indent the while loop once (default is 4 spaces) and everything inside of the while loop twice (default 8 spaces). When pasting a code like this, make sure to include all code needed to run. In this case, show your import re (I had to find out myself you imported that).
I hope I helped.
You are comparing an int(1) to str("1").
It should be
if int(ask) == 1:
# rest of your code
or you can use raw_input() function which returns a string value
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to write a code where by the code will ask users to input value into the following keys;
Name, Age, Course.
Note that the space for user input can be between 1 and 20.
Then I will be able to print the keys and the values EVEN IF the users input is not up to 20 but did not exceed 20 users
Not sure what you are trying to acomplish here exactly. The following codes
creates a dictionary with all the information as keys.
user_dict={}
counter=1
while True:
print ("Enter data for user: ",counter)
name= input("enter name")
age = input("enter age")
course = input("enter course")
user_dict[(name,age,course)]=counter
choice = input("do you want to continue y:yes/press any other key to quit")
if choice=="y" or choice=="Y":
break
If you want to check if a person of a specific age has taken a course, just do
print (("daniel","20","Data Structures") in user_dict)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
need help with taking inputs in a a loop like so.
example = input("enter the example !")
Then I need to add that input to one single variable and later print it all out
on separate lines EG:
loop cycle 1:
enter the example ! test1
loop cycle 2:
enter the example ! test2
loop cycle 3:
enter the example ! test3
inputs:
1. test1
2. test2
3. test3
One thing is that I am unable to use .append due to using lists in my case is
not in max efficiency. (probably will need to be taught to use \n)
you can append new line character to input function
for python2.
example = ""
for i in range(1,4):
example = example + str(i)+". " +raw_input("enter the example !") +"\n"
print example
for python3
example = ""
for i in range(1,4):
example = example + str(i)+". " +input("enter the example !") +"\n"
print (example)
output
messi#messi-Hi-Fi-B85S3:~/Desktop/soc$ python sample.py
enter the example !text 1
enter the example !text 2
enter the example !text 2
1. text 1
2. text 2
3. text 2
You can append the additional inputs onto the same string using the += operator
You will however need to declare the variable before using += for the first time
For example:
example = ""
for num in range(1,4):
print "loop cycle {}:".format(num)
example += "{}. {}".format(num, input("enter the example !"))
if num < 3:
example += "\n"
print example
edit: updated to include conditional new lines and input numbers
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm fairly new to python and I was wondering if someone was able to help me with looping this block of code and I did looping in the past but this one involves a list element having to change every time 1 through to 10 and I don't know how I could make that change.
print ("Question 1: ")
print (questions[0])
#asks for the answer from the user
ans = int(input())
#compares the users input to the answer
if ans == eval(questions[0]):
print ("Well done, you got it correct")
#if correct grants a point to the overall score
score = score + 1
The closest way to do so while maintaining your code is the following
for index, question in enumerate(questions):
print ("Question {}: ".format(index+1))
print (question)
#asks for the answer from the user
ans = int(input())
#compares the users input to the answer
if ans == eval(question):
print ("Well done, you got it correct")
#if correct grants a point to the overall score
score = score + 1
Note that you should avoid using eval because it is unsafe. Recommended alternatives are to either make a dictionary with pre-baked questions and answers, e.g.
questions = {'What is 5 + 4?' : 9,
'What is 3 - 1?' : 2}
Or programmatically come up with questions and answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I apologize for the title not being clear, anyways how do I create a loop that will put numbers into a string, what i mean is (this is from the assignment im doing):
Enter number of courses:
Enter grade for course no. 1:
Enter weight for course no. 1:
Enter grade for course no. 2
......
Enter grade for course no. 7
......
Enter weight for course no. 9
So how would i do something like this, i have to create an input statement which has numbers in order (1-infinity but limited by number of courses). Thank you if you need anymore information or for me to clarify let me know.
You can use string formatting:
noc = int(input('Enter number of courses: '))
for i in range(1, noc+1):
grade = input('Enter grade for course no. {}: '.format(i))
weight = input('Enter weight for course no. {}: '.format(i))
#do something with grade and weight here.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I have a script that I wrote in python for testing out the sorting algorithms that I've implemented. The main part of the program asks the user to select one of the sort algorithms from a list. And then whether they would like to sort from a file of numbers or select a list of random numbers. I have it set up (I think) so that typing a number that's not in the first list of choices will just print "Bad choice" and try getting the number again.
[Edit] After taking the advice of the answer I changed a couple of the inputs to raw inputs. And I changed the structure of the program. It now works perfectly except it still prints out "Bad Choice" even after a success.
def fileOrRandom():
return raw_input("Would you like to read from file or random list? (A or B): ")
choices = {1:SelectionSorter,2:ComparisonSorter}
print "Please enter the type of sort you would like to perform."
print "1. Selection Sort\n2. Comparison Sort"
while(True):
try:
choice=input("Your choice: ")
for i in range(2):
if(choice==i+1):
choice2=fileOrRandom()
if(choice2=='A'):
fileName=raw_input("Please Enter The Name of the File to sort: ")
sorter = choices[choice](fileName)
sorter.timedSort(sorter.sort)
elif(choice2=='B'):
num = input("How many random numbers would you like to sort: ")
sorter = choices[choice](None,num)
sorter.timedSort(sorter.sort)
else:
raise Exception
else:
raise Exception
break
except Exception:
print "Bad Choice"
My issue is that it works as expected for the first part where it will return "bad choice" for a number that's not in the list, and it will get fileOrRandom(), but it still prints "Bad Choice" when selecting good values that should print out my results because sorter.timedSort(sorter.sort) executes my sorting algorithm and prints out a bunch of other things to the screen. Am I just missing something simple or is there a better way to deal with these nested options in a Python program?
use raw_input()
def fileOrRandom():
return raw_input("Would you like to read from file or random list? (A or B): ")
your while loop should look like this (after fixing the indentation)
while True :
choice=raw_input("Your choice: ")
for i in range(2):
if choice==i+1 and fileOrRandom()=="A" :
fileName=raw_input("Please Enter The Name of the File to sort: ")
sorter = choices[choice](fileName)
sorter.timedSort(sorter.sort)
elif choice==i+1 and fileOrRandom()=="B" :
num = raw_input("How many random numbers would you like to sort: ")
sorter = choices[choice](None,num)
sorter.timedSort(sorter.sort)
elif choice in ['q','Q']: break
else: print "Bad choice"