how to fix syntax error while if...else statement? - python

import webbrowser
Character_Name = "Random"
Character_Age = "14"
input("Name of the person you want the ID of.")
print("Here's The Information That I Have: Name:" + Character_Name + ", Age:" + Character_Age + ", Available socialmedia accounts: (insta) www.instagram.com/asenpai369")
a = input("Should i open the link in your web browser?")
if a : "Yes"
webbrowser.open("www.instagram.com/idksoumyadeep")
else:
print("okay, if its a mistype then please type it again")
and the error
else:
^
SyntaxError: invalid syntax
please help thanks in advance :))

It should be
if a == "Yes": webbrowser.open("www.instagram.com/idksoumyadeep")
else: print("okay, if its a mistype then please type it again")

Use indentation after the if and else:
import webbrowser
Character_Name = "Random"
Character_Age = "14"
input("Name of the person you want the ID of.")
print("Here's The Information That I Have: Name:" + Character_Name + ", Age:" + Character_Age + ", Available socialmedia accounts: (insta) www.instagram.com/asenpai369")
a = input("Should i open the link in your web browser?")
if a == "Yes":
webbrowser.open("www.instagram.com/idksoumyadeep")
else:
print("okay, if its a mistype then please type it again")

Related

Python input issue?

name_list = []
command_list = ["Add" ,"Help"]
def start():
input("Hello please type in a command : ")
start()
if (input == "help" or "Help"):
print("Here are the following commands for this program : ")
i = 0
for i in command_list :
print("'" + i + "'")
start()
if (input == "Add" or "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
So this code is causing me a bit of trouble , upon starting the code everything works fine it asks me to type in a command as it should , and if i type in 'help' it does exactly what its supposed to do, which is list all the commands, after that its supposed to reset via the start() command and once again ask me to input a command , however this time no matter what i write it activates this block of code:
if (input == "Add" or "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
Can someone please help me fix this and or explain why this is happening??
Thank you in advanced.
You have to use a while loop here, also repr(i) more efficient than "'" + i + "'":
name_list = []
command_list = ["Add" ,"Help"]
def start():
return input("Hello please type in a command : ")
a=start()
while a.lower()=='help':
print("Here are the following commands for this program : ")
i = 0
for i in command_list :
print(repr(i))
a=start()
if a.lower()=='add':
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
else:
print('invalid input')
Example output:
Hello please type in a command : help
Here are the following commands for this program :
'Add'
'Help'
Hello please type in a command : Help
Here are the following commands for this program :
'Add'
'Help'
Hello please type in a command : Add
Insert name please : Bob
Welcome Bob
and also:
print(name_list)
Returns:
['Bob']
Your code has several issues. Here's a version that works.
name_list = []
command_list = ["Add", "Help"]
def start():
return input("Hello please type in a command : ")
name = ""
while not name:
command = start()
if (command.lower() == "add"):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
# printing help for everything except add
else:
print("Here are the following commands for this program : ")
i = 0
for i in command_list:
print(repr(i))
if (input == "help" or "Help"):
print("Here are the following commands for this program : ")
i = 0
for i in command_list :
print("'" + i + "'")
start() # <--- your call to start() function does not reset where the interpreter left off reading you code.
Python interpreter simply reads each one of your lines and does something with it. In your if statement, the line start() is called, it ask for an user input and returns None. Then this if statement is complete, then the next line read is as you described:
if (input == "Add" or "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
Solution to your question:
name_list = []
command_list = ["Add" ,"Help"]
def start():
input("Hello please type in a command : ")
while True:
start()
if (input == "help" or "Help"):
print("Here are the following commands for this program : ")
i = 0
for i in command_list :
repr(i)
if (input == "Add" or "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
if input == "something for break condition":
break
name_list = []
command_list = ["Add" ,"Help"]
def start():
inpt = input("Hello please type in a command : ")
if (inpt == "help" or inpt == "Help"):
print("Here are the following commands for this program : ")
print(command_list)
start()
elif (inpt == "Add" or inpt == "add" ):
name = input("Insert name please : ")
print("Welcome " + name)
name_list.append(name)
yn = input("Do you want to add another person? ")
if yn in ['y','Y','yes','Yes','YES']:
start()
else:
print ("Quitting!! all the people entered are")
print(name_list)
return (None)
else:
print("Please type 'help' for help")
start()

Program closes abruptly at if statement

This is the part of my code that doesn't work. The else statement works fine but when the password is wrong the program just closes abruptly.
if logged_in == 'admin':
tries = -3
while tries < 0:
password = input("enter supersecret password. ")
if password != 'strange alien colour':
print("incorrect password. Try again. " + tries + " tries left.")
tries = tries + i
else:
print("Hello admin, would you like to see a status report?")
input()
import sys
sys.exit(0)
This is the entire code:
users = ['anonymous', 'me', 'bill', 'me123']
i = 1
while i == 1:
logged_in = input("Username:\n")
if logged_in == 'admin':
tries = -3
while tries < 0:
password = input("enter supersecret password. ")
if password != 'strange alien colour':
print("incorrect password. Try again. " + tries + " tries left.")
tries = tries + i
else:
print("Hello admin, would you like to see a status report?")
input()
import sys
sys.exit(0)
if logged_in in users:
print ("Hello " + logged_in + ", welcome back!")
break
else:
print ("invalid username. Do you wish to create an account? (Y/N)\n")
create_account = input()
if create_account == 'Y' or create_account == 'y':
new_username = input("Enter new username: ")
print("You have creted a new account. Welcome, " + new_username)
users.append(new_username)
else:
print ("Goodbye.")
break
input()
Your error is that on this line
print("incorrect password. Try again. " + tries + " tries left.")
tries is an int, whereas the other parts are strings. As you cannot add these two types, you get an error which is
TypeError: must be str, not int
To fix this just change the line to
print("incorrect password. Try again. " + str(tries) + " tries left.")
On an unrelated note, it is usually recommended that imports are at the top of your program.
print("incorrect password. Try again. " + tries + " tries left.")
This is incorrect as tries is an integer and therefore cannot be concatenated with the two other parts in the quotation marks as they are strings and needs to be converted into a string first to do so. eg.
print("incorrect password. Try again. " + str(tries) + " tries left.")

Why is it saying access denied when I input the username correctly?

Hey I have wrote this code however I cannot see what is wrong with it, it is saying that the username is wrong yet if I print it, it returns exactly what i input.
ea = input("Do you already have an account")
if ea == "Yes" or ea == "yes":
Ausername = input("Input your username")
Apassword = input("Input your password")
f=open("login.txt","r")
lines=f.readlines()
username=lines[0]
if (Ausername) == (username):
print("Welcome to the quiz")
else:
print("Access denied")
f.close()
else:
name = input("Input your name")
yeargroup = input("Input your year group")
age = str(input("Input your age"))
firstusername = ((name[0]+name[1]+name[2])+(age))
print((firstusername)+(" is your username"))
firstpassword = input("Enter what you want your password to be")
print(firstusername)
print(firstpassword)
login = open("login.txt","a")
login.write(firstusername + "\n" + name + "\n" + yeargroup + "\n" + age + "\n" + firstpassword + "\n")
login.close()
print("---------------------------------------------------")
File data is read in by default with the trailing newline. Did you try calling str.strip before comparing your strings?
if Ausername == username.strip():
...
Also, if you want to do case insensitive comparisons, you should convert your string to lowercase using str.lower to reduce the size of your search space:
if ea.lower() == "yes":
...

Python Syntax Error: expected an intended block

So first i clicked on Run Module
Then this came up
My code
import time
print("First we need to know you.")
print("Enter your name please.")
time.sleep(2)
name = input("Name: ")
print("Welcome, " + name + "!")
time.sleep(3)
criminal = input("Are you a criminal?: ")
if criminal=='Y':
Right here it highlights print as red
print('Oh no')
elif criminal=='N':
print('Thank god!')
You need to indent after an if and the elif:
if criminal=='Y':
print('Oh no')
elif criminal=='N':
print('Thank god!')
Also, don't indent after the import:
import time
print("First we need to know you.")
You have to indent the print('Oh no'):
if criminal=='Y':
print('Oh no')
elif criminal=='N':
print('Thank god!')

local variable referenced before assignment python issue

So I'm coding a small project and I'm struggling with a certain aspect so far.
Here is the code:
import re
def clientDetails():
print("Welcome to the InHouse Solutions Room Painting Price Calculator")
print("STEP 1 - CLIENT DETAILS")
print("Please enter your full name")
userName = input(">>>")
print("Please enter your post code")
postCode = input(">>>")
print("Is your house a number, or a name?")
nameOrNumber = input(">>>")
if nameOrNumber == "number" or nameOrNumber == "Number":
print("Please enter your house number")
houseNumber = input(">>>")
elif nameOrNumber == "Name" or nameOrNumber == "name":
print("Please enter your house name")
houseName = input(">>>")
else:
print("Invalid")
house = (houseNumber) + (houseName)
address = (postCode) + ", " + (house)
print("Thank you for your information")
print (userName)
print (address)
print (" ")
print ("Is this information correct? Pleast enter Yes or No")
clientDetailsCorrect = input(">>>")
if clientDetailsCorrect == "no" or clientDetailsCorrect == "No":
clientDetails()
clientDetails()
Not sure what's going wrong as I haven't actually referenced the variable anywhere else. Someone help.
It would help if you posted the traceback.
That said, this line is the likely source of the problem:
house = (houseNumber) + (houseName)
The way your code is currently written, only one of houseNumber or houseName will be defined. So Python is likely complaining about the missing one.
Given how your code looks so far, it's probably better to just do:
print("Please enter your house name or number")
house = input(">>>")
And remove the house = (houseNumber) + (houseName) line.
Try this:
def clientDetails():
print("Welcome to the InHouse Solutions Room Painting Price Calculator\n")
print("STEP 1 - CLIENT DETAILS")
print("Please enter your full name")
userName = raw_input(">>>")
print("Please enter your post code")
postCode = raw_input(">>>")
print("Please enter your hose number or name")
house = raw_input(">>>")
address = "{}, {}".format(postCode, house)
print("Thank you for your information.\n")
print (userName)
print (address)
print (" ")
print ("Is this information correct? Pleast enter Yes or No")
clientDetailsCorrect = raw_input(">>>")
if clientDetailsCorrect.lower().startswith('n'):
clientDetails()
Using raw_input is better, it will input everything as a string. Also it will allow users to not type quotations to input text (I assume you will run this from CLI). If you later on need to separate houses that are numbers from names Python has very good string methods you can use to do so many wonderful things, I used a couple of them to simplify your code :)
N

Categories

Resources