Indented block error on "Elif" part of assignment - python-3.x

Ok, My TA (Teachers Assistant) E-mailed me back and told me the code I was looking for in my previous post. I am trying to get various letters and symbols to become turtle commands. Here is my current code:
import turtle
command = str(input("enter a command line"))
length = int(input("enter a length"))
angle = float(input("enter an angle"))
commandLength = len(command)
for i in range(commandLength):
if(command[i] == 'h'):
elif(command[i] == 'h'):
OK, My TA was E-mailing me from his cell phone, so he probably couldn't be real specific. But he told me to copy everything in my "if" command line and just put "ELIF" in front of it. When I do that I get an "indentation error" "expected an indented block" The error is on the line my elif statement is on. Anyone who knows me by now, knows how utterly new I am to programming. Can someone tell me why I am getting the error, and how to fix it or if I am writing this correctly. Thank You.

import turtle
command = str(input("enter a command line"))
length = int(input("enter a length"))
angle = float(input("enter an angle"))
commandLength = len(command)
for i in range(commandLength):
if(command[i] == 'h'):
elif(command[i] == 'h'):
Every block should be indented.
A block is everything with : at the end.

Related

Does someone know to fix this error? I don't know its for a note on school

For school I need to write a code that will play Hangman with you but, there is a error which I can't fix. The error is in the part of the print, I think it has something to do with the import but I am not sure (the error is a invalid syntax)
I tried to put the print and the message on different lines but I didn't work, I also tried a colon behind the print but also that didn't solve the problem (I am in Python 3.6.1 on repl.it)
https://repl.it/#Informatica132/Galgje
It has to print: "Your right" Character "is in the chosen word", or "Your wrong" Character "is not in the chosen word" but it prints a syntaxerror invalid syntax.
Errors, errors everywhere. I have fixed all the syntax errors, but you still need to figure out your program logic. (Currently it lets you guess a single letter before exiting.)
import random
Trys = 0
Words = ["Hey" , "Hello" , "mate" , "victory" , "wineglass" , "UK"]
Word = (random.choice(Words))
Character = str(input("type one character to look if it is in the chosen word"))
if Character in Word:
print ("Your right", Character, "is in the chosen word")
Trys = Trys + 1
else:
print ("Your wrong", Character, "is not in the chosen word")
Trys = Trys + 1

Code not checking if inputted answer is correct

I am trying to create a multiple choice quiz that takes questions from an external .txt file and prints it in python. The text file is laid out like this:
1,Who was the first man to walk on the moon?,A.Michael Jackson,B.Buzz Lightyear,C.Neil Armstrong,D.Nobody,C
When I run the code and input the right answer it still says incorrect but continues to say the answer I inputted.
In the code I split each line in the text file by a ',' so the correct answer in the text file is always detail[6]. In the code I have put:
if answer.upper() == detail[6]:
print("Well done, that's correct!")
score=score + 1
print(score)
elif answer.upper() != detail[6]:
print("Incorrect, the correct answer is ",detail[6])
print(score)
I thought this would work as it is checking the inputted answer against detail[6] but it always comes out as incorrect.
import random
score=0
with open('space_quiz_test.txt') as f:
quiz = f.readlines()
questions = random.sample(quiz, 10)
for question in questions:
detail = question.split(",")
print(detail[0],detail[1],detail[2],detail[3],detail[4],detail[5])
print(" ")
answer=input("Answer: ")
while True:
if answer.upper() not in ('A','B','C','D'):
print("Answer not valid, try again")
else:
break
if answer.upper() == detail[6]:
print("Well done, that's correct!")
score=score + 1
print(score)
elif answer.upper() != detail[6]:
print("Incorrect, the correct answer is ",detail[6])
print(score)
I would like the code to be able to check if the inputted answer is correct by checking it against detail[6] within the text file, instead of always coming out as incorrect, the correct answer is detail[6].
The problem is that readlines() retains the newline character at the end of each line.
Your detail[6] is something like 'C\n' rather than 'C' itself. To fix that, use
detail = question.strip().split(",")

Why does error 'unexpected character after line continuation character' appear here?

Does someone know what is wrong with this section of my code as it seems to cause errors. I'm new to programming so I'm not completely sure what's wrong.
menu = "Be Lenny's Friend?\n"
1. Yes\n\
2. No\n\
answer = int(input(menu))
if answer == 1:
print(" ( ͡° ͜ʖ ͡°): Yayyyy! We are going to be friends!")
elif answer == 2:
reason = input(" ( ͡° ʖ̯ ͡°): Why do you not want to be my friend :(")
Error message:
'unexpected character after line continuation character'
Here you have set the variable as a tuple of a string and... well that’s where things get confusing.
The backslash is a like continuation symbol as in you can then break the line and continue on and it would count as the same line. However what the interpreter sees is n: which makes no sense. That is what it is complaining about.
If you wanted to add a new line to the string itself, you could add the \n at the end of the string.
However, also note that if you printed the string using print in the vanilla form without any other arguments than the Adrianne itself, it will append a new line automatically. So if you do add the \n, it may still not be what you want when you print it out. Parameter can of course be changed in the print function to take care of that.
I'm not sure what you are trying to archive with ,\n\: at the end of the line, but this line of code will remove the syntax error:
menu = "Be Lenny's Friend?"
If you want to archive a new line after the string you need to move "\n" into the string like this:
menu = "Be Lenny's Friend?\n"
Edit:
This should work for you:
menu = "Be Lenny's Friend?\n\t1. Yes\n\t2. No\n"
answer = int(input(menu))
if answer == 1: print(" ( ͡° ͜ʖ ͡°): Yayyyy! We are going to be friends!")
elif answer == 2: reason = input(" ( ͡° ʖ̯ ͡°): Why do you not want to be my friend :(\n")

How can i check to see if somethings already written to a txt and give an error if so python

I am doing a school project and I have to make a voting system that uses a voting code. I need help with the code that opens up the 2 files, checks to see if the code is there and gives a value error if it is.
while True:
Code = input("Enter your 6 digit code: ")
try:
Code = int(Code)
if "0" in str(Code): break
if len(str(Code)) != 6 : raise ValueError
else: break
readt = open("Trump.txt" , "r")
readh = open("Clinton.txt" , "r")
readhh = readh.read()
readtt = readt.read()
if Code in str(readtt) or Code in str(readhh): raise ValueError
else: break
readt.close()
readh.close()
except ValueError:
print("Invalid")
Here are a couple pointers to fix your program:
The if len ... else part seems to leave the while loop either through raise or break. The code that does open is never executed.
Also you call open a lot of times. This will become problematic because leaking file descriptors is a problem. Use the with open(...) statement for this. This way, you cannot leave the file open by accident. Your close statements are behind another if ... else construction that will leave the loop in every case.
Your variable names are a bit opaque, perhaps you want to invent some more telling ones.
Why are there two files? Shouldn't there be only one file that contains all the used codes?
Assuming that you presented all the information in your question this is the solution for your problem:
def code_checker():
codes = []
with open('Trump.txt', 'r') as f1:
for line in f1:
codes.append(line.rstrip())
with open('Clinton.txt', 'r') as f2:
for line in f2:
codes.append(line.rstrip())
code = input('Enter your 6 digit code:\n')
while True:
if '0' in code or len(code) != 6:
print('Invalid code\n')
code = input()
continue
elif code in codes:
raise ValueError
code_checker()

.readline not printing a letter instead of a line

keywords = open("keywords.txt", "r")
problem = input("Please give us a simple description of the problem you are having with your phone, so we can identify the solution and give it to you...")
if "screen" in problem:
print(keywords.readline())
elif "water" in problem:
print(keywords.readline(2))
elif "battery" in problem:
print(keywords.readline(3))
elif "dropped" in problem:
print(keywords.readline(4))
keywords.readlines() is what you are looking for.
keywords.readline() is taking only one line so that if you say keywords.readline(1) It will return second letter in first line

Resources