The Student Room Group

FREE Programming/Coding Help Desk

Scroll to see replies

Original post by Jooooshy
When I first started programming I preferred to use a simple editor, like Atom or Sublime Text, since I was forced to learn APIs and didn't have to rely on IDE autocorrections/suggestions. Although, it's not necessarily a bad idea to start with an IDE and I agree with Async that Jetbrains' products are usually very good.


thank you plase can you tell me a bit more about APIs.
and why you were forced to learn them? (not trying to get into your business)
im just trying to understand their benefit

thank you
Hello!

I need help with validation for my python code.
text=input("What is you favourite animal?" )
word=['wolf' , 'cat' , 'lion' , 'zebra']
if word in text:
print("Your favourite animal is a mammal." )

Here is another example

print(" y is yes and n is no" )
food= input("Do you like chocolate cake?" )
if food== 'y' : ## what if the user inputs Y or yes instead of y, it won't work that's why i need help.
print(" Me too" )

I basically need help with validation so if the user input is in capital letters or has punctuation or i guess even typos it should still work. I need help doing this.



Thanks
(edited 8 years ago)
For the first example, its
if text in word:
not the other way around.

For the second one, do
if food.lower() == "y":

.lower() basically makes it all lowercase, so it doesnt matter if the person inputs Y or y, it will work.
Original post by peterxz
For the first example, its
if text in word:
not the other way around.

For the second one, do
if food.lower() == "y":

.lower() basically makes it all lowercase, so it doesnt matter if the person inputs Y or y, it will work.


ohh okay thanks i get it now!
What is the the user inputs yes or yeah instead of y?
With the current code, it won't do what you want it to do. If you want to account for that, you would have to define an array, containing all the possible inputs (yes, yeah, etc) and then do
if food in myList:
similalry to your solution in the first part.
However, I don't suggest you do this, unless absolutely neccessary (or you are simulating human conversation), as it is easier to define one no and one yes for the user and keep asking for any of the expected inputs until they input something you asked them for.
Reply 585
Original post by bluepearl7
Hello!

I need help with validation for my python code.
text=input("What is you favourite animal?" )
word=['wolf' , 'cat' , 'lion' , 'zebra']
if word in text:
print("Your favourite animal is a mammal." )

Here is another example

print(" y is yes and n is no" )
food= input("Do you like chocolate cake?" )
if food== 'y' : ## what if the user inputs Y or yes instead of y, it won't work that's why i need help.
print(" Me too" )

I basically need help with validation so if the user input is in capital letters or has punctuation or i guess even typos it should still work. I need help doing this.



Thanks


Your problem is that you're using "Yoda Conditions/Expressions".
so you're doing
if 1 == variableName:

when you should be doing
if variableName == 1:
Where exactly did he do this? In the first example he got confused with his variable names and the second one has got nothing to do with what you are talking about...
hey i need help on task 2 and 3 its getting the speeding reg plates into a text file that i am having trouble with i will pm the code to anyone who is willing to help
Original post by CheshireCatt24
hey i need help on task 2 and 3 its getting the speeding reg plates into a text file that i am having trouble with i will pm the code to anyone who is willing to help


Which part are you having issues with? I'll help.
m = 1
my_list_1 = [2 , 4, 1]
for x in my_list_1:
for y in range(1,3):
if (x + y) % 3:
m = m * x
print (m)

the part i highlighted is my problem
this is the first time i have seen sometime different to the normal whereby the conditon is to see if the remainder is 0 or not.

but for the highlighted if condition there is no such comparison
i wanted to ask what is the if statement doing exactly

thank you
Original post by bigmansouf
m = 1
my_list_1 = [2 , 4, 1]
for x in my_list_1:
for y in range(1,3):
if (x + y) % 3:
m = m * x
print (m)
the part i highlighted is my problem
this is the first time i have seen sometime different to the normal whereby the conditon is to see if the remainder is 0 or not.

but for the highlighted if condition there is no such comparison
i wanted to ask what is the if statement doing exactly

thank you


The if statement is seeing if x+y is divisible by 3 - seeing if the remainder is 0 or not. You don't need to specify "X MOD = 0" in the if statement because % essentially means divisible which makes you deduce the fact that remainder will indeed be 0. Basically, specifying that the modulus have to be equal to zero is redundant because python knows it has to be anyway.
Original post by peterxz
Basically, specifying that the modulus have to be equal to zero is redundant because python knows it has to be anyway.


You've got it the wrong way around. This is checking if the result is not 0.
Original post by peterxz
The if statement is seeing if x+y is divisible by 3 - seeing if the remainder is 0 or not. You don't need to specify "X MOD = 0" in the if statement because % essentially means divisible which makes you deduce the fact that remainder will indeed be 0. Basically, specifying that the modulus have to be equal to zero is redundant because python knows it has to be anyway.


This has little to do with the modulus operator and more to do with the fact that when converting integers to boolean values, 0 becomes False and all other integers become True.
Hi! I need help with python, i am a beginner so please help! I need some help in recognizing multiple words.

w1=['wolf' , 'lion' , 'elephant']
w2=['chicken' , 'cuckoo']

question=str(input("What is you favourite animal?"))

if question.lower()in w1:
print("your fav animal is a mammal")
elif question.lower() in w2:
print("your fav animal is a bird")

If i type in single words as the input like wolf or chicken , the code works but my problem is if i enter 'my favourite animal is a lion', it won't work. So python isn't recognizing the words if its with other words.why? and how do i fix this? if you can think of any other working methods that recognize multiple words in python, please tell me!
Please help , thanks!
Original post by bluepearl7
Hi! I need help with python, i am a beginner so please help! I need some help in recognizing multiple words.

w1=['wolf' , 'lion' , 'elephant']
w2=['chicken' , 'cuckoo']

question=str(input("What is you favourite animal?":wink:)

if question.lower()in w1:
print("your fav animal is a mammal":wink:
elif question.lower() in w2:
print("your fav animal is a bird":wink:

If i type in single words as the input like wolf or chicken , the code works but my problem is if i enter 'my favourite animal is a lion', it won't work. So python isn't recognizing the words if its with other words.why? and how do i fix this? if you can think of any other working methods that recognize multiple words in python, please tell me!
Please help , thanks!


The easiest way to do this would be splitting the "question" variable to make a FOR loop for each word, and then check if that word is in the "w1" or "w2" list, like so:

w1=['wolf' , 'lion' , 'elephant']
w2=['chicken' , 'cuckoo']

question=str(input("What is you favourite animal?"))

for word in question.split():
if word in w1:
print("your fav animal is a mammal")
elif word in w2:
print("your fav animal is a bird")
(edited 8 years ago)
Original post by thebaco123
The easiest way to do this would be splitting the "question" variable to make a FOR loop for each word, and then check if that word is in the "w1" or "w2" list, like so:
w1=['wolf' , 'lion' , 'elephant']
w2=['chicken' , 'cuckoo']

question=str(input("What is you favourite animal?":wink:)

for word in question.split():
if word in w1:
print("your fav animal is a mammal":wink:
elif word in w2:
print("your fav animal is a bird":wink:



THANKYOU!!

i've been trying to fiigure this out for a few hours and my code finally works now!
Thankyou so much :smile:
Hi again!

y=['yes' , 'yeah' , 'yep' , 'y' , 'yup']
n=['no' , 'nah' , 'nope']

def work():
a=input("is this correct?")
if a==y:
print("Have a great day,Good bye!")
elif a==n:
print("We are sorry.")
else:
work()

so my problem here is with my data validation
if the user doesn't enter y or n then the work will be called again

bit its stuck in an infinite loop!
Even if i input y it still calls the function again.

and after that if the user keeps entering the incorrect answer for 10 times i wanna automatically exit the programme?
I can use a for loop? but how?
Please help!
Original post by bluepearl7
Hi again!

y=['yes' , 'yeah' , 'yep' , 'y' , 'yup']
n=['no' , 'nah' , 'nope']

def work():
a=input("is this correct?":wink:
if a==y:
print("Have a great day,Good bye!":wink:
elif a==n:
print("We are sorry.":wink:
else:
work()

so my problem here is with my data validation
if the user doesn't enter y or n then the work will be called again

bit its stuck in an infinite loop!
Even if i input y it still calls the function again.

and after that if the user keeps entering the incorrect answer for 10 times i wanna automatically exit the programme?
I can use a for loop? but how?
Please help!


You're checking if the string the user has input into a is equal to your list of strings. You need to check if their input is in your list instead.

Easiest way to exit would be to have a variable to count the number of times they have been asked, and just before calling your work() function increment it and check if it is greater than 10.
Hi am doing computer science in pascal but i was wondering if anyone could explain a line of code for me. Thanks

If Orientation = 'v' Then
Begin
For Scan := 0 To Ship.Size - 1 Do
If Board[Row + Scan,Column] <> '-' Then
Valid := False;

I get most of it just not "If Board[Row + Scan,Column] <> '-' Then".
(edited 8 years ago)
Original post by Push_More_Button
You're checking if the string the user has input into a is equal to your list of strings. You need to check if their input is in your list instead.

Easiest way to exit would be to have a variable to count the number of times they have been asked, and just before calling your work() function increment it and check if it is greater than 10.


It still doesn't seem to work but it still doesn''t work, probaby something that i've done wrong. Please give me suggestions on how i can improve this
thanks

y=['yes' , 'yeah' , 'yep' , 'y' , 'yup']
n=['no' , 'nah' , 'nope']

def work() :
a=input("Is this correct?" )
if a in y:
print("Have a great day,Good bye!" )

elif a in n:
print("We are sorry." )

else:
i=1
for i in range (3) :
work()
i=i+1
if i >= 3 :
print("bye!!" )
raise SystemExit

Quick Reply

Latest

Trending

Trending