Replace expected at least 2 arguments got 1 error - python

Hello I got some stuck in my code and I tried but I still don't know how to fix it. Here it's my code.
def staff_info (name):
print ("So you are " + name + ", years old. ")
name = input ("Type your name: ")
confirm_info = input ("So you have " + name + ". Confirm that? (Yes/No) ")
if confirm_info == "Yes":
print ("Okay so I have few more question for you. ")
else:
confirm_info == "No"
change_info = input ("So what do you want to change? ")
if change_info == "name":
name_change = input ("Type the name you want to change: ")
name = name.replace (name_change) #error here
...
else:
...
print ("So you are " + name + ", " + age + " years old. You have over " + result_experience1 + " with code.")
If you could explain it and give me a command to solve it, I'd appreciate it. My adventure with Python beginning last week from now. Thanks you.

Thereplace method takes two mandatory parameters:
The substring you want to replace
The new value
So, change this
name = name.replace (name_change)
to
name = name.replace(name, name_change)
More here

You can try this:
def staff_info(name):
print("So you are " + name + ", years old. ")
name = input("Type your name: ")
confirm_info = input("So you have " + name + ". Confirm that? (Yes/No) ")
if confirm_info == "Yes":
print("Okay so I have few more question for you. ")
elif confirm_info == "No":
change_info = input("So what do you want to change? ")
if change_info != name:
name_change = input("Type the name you want to change: ")
new_name = name_change.replace(name, name_change)
print("New Name = ", new_name)
else:
print("...")

Related

I want my python code to restart if variable contain is "No" [duplicate]

This question already has answers here:
How do I repeat the program in python [duplicate]
(2 answers)
Closed 2 years ago.
nameQuestion = "Whats ur name?"
print(nameQuestion)
name = input()
ageAuestion = "How old r u?"
print(ageAuestion)
age = input()
print("Your name is: " + name + " " + "age: " + age + "?(yes or no)")
answer = input()
if answer == "Yes":
print("Name: " + name + ", " + "Age: " + age)
else:
print("Error")
I need to restart code if answer == "no". How i can do it
This will do:
while True:
nameQuestion = "Whats ur name?"
print(nameQuestion)
name = input()
ageAuestion = "How old r u?"
print(ageAuestion)
age = input()
print("Your name is: " + name + " " + "age: " + age + "?(yes or no)")
answer = input()
if answer == "Yes":
print("Name: " + name + ", " + "Age: " + age)
break
else:
print("Error")
continue
While Doc
use while True and put a check when you want to break out of while loop.
while True:
`your code`
.
.
`condition to break out of while loop`
Use a while loop:
answer = ''
while answer != 'yes':
nameQuestion = "Whats ur name?"
print(nameQuestion)
name = input()
ageAuestion = "How old r u?"
print(ageAuestion)
age = input()
print("Your name is: " + name + " " + "age: " + age + "?(yes or no)")
answer = input()
print("Name: " + name + ", " + "Age: " + age)
while True:
nameQuestion = "Whats ur name?"
print(nameQuestion)
name = input()
ageAuestion = "How old r u?"
print(ageAuestion)
age = input()
print("Your name is: " + name + " " + "age: " + age + "?(yes or no)")
answer = input()
answer = answer.lower()
if answer == "yes":
print("Name: " + name + ", " + "Age: " + age)
break
else:
pass
You can use while loop
You can do something like this:
wrong_data = True
while wrong_data:
name = input("Whats ur name?")
age = input("How old r u?")
answer = input(f"Your name is {name} and your age is {age}?(yes or no)")
# you should put answer.lower so both 'yes' and 'Yes' are interpreted correctly
if answer.lower() == "yes":
print(f"Name: {name}, Age: {age}")
wrong_data = False

'while' loop in Python

I'm starting to learn Python by throwing myself in the deep end and trying a bunch of exercises.
In this particular exercise, I'm trying to make a program to ask the name and age of the user and tell the user the year they're going to turn 100.
I tried putting in a yes/no input to ask if the user's birthday has passed this year, and give the correct year they will turn 100, based on that.
I wanted to incorporate a while loop so that if the user enters an answer that isn't "yes" or "no", I ask them to answer either "yes" or "no". I also wanted to make the answer case insensitive.
print("What is your name?")
userName = input("Enter name: ")
print("Hello, " + userName + ", how old are you?")
userAge = input("Enter age: ")
print("Did you celebrate your birthday this year?")
while answer.lower() not in ('yes', 'no'):
answer = input ("Yes/No: ")
if answer.lower() == 'yes':
print ("You are " + userAge + " years old! That means you'll be turning 100 years old in the year " + str(2019 - int(userAge) + 100))
elif answer.lower() == 'no':
print ("You'll be turning " + str(int(userAge) + 1) + " this year! That means you'll be turning 100 years old in the year " + str(2019 - int(userAge) + 99))
else:
print ('Please type "Yes" or "No"')
print ("Have a good life")
You should addd the input before trying to access the answer.lower method, so it will be something like:
answer = input('Yes/No: ')
while answer.lower() not in ('yes','no'):
answer = input('Yes/No: ')
Check geckos response in the comments: just initialize answer with an empty string

My code is acting weird, it is pulling information if statements when not told to [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
I have recently started learning Python, and the experience has been great, however, last night, I tried to make a simple program, and it is acting very strange
I have tried to change the locations of variables, and what they store/handle, and nothing I try seems to be working
def RF():
user_type = input("Are you a new user? ")
if user_type is "Yes" or "yes":
ID = random.randrange(1, 999999)
print("Your new ID Number is: " + str(ID))
name = input("Please enter your name: ")
password = input("Password: ")
ID = input("Account ID: ")
writtingID = open('Acc info.txt', 'w')
writtingID.write("ID: " + str(ID) + " | NAME: " + name + " | PASS: " + password)
writtingID.close()
elif user_type is "No" or "no":
name = input("Please enter your name: ")
password = input("Password: ")
ID = input("Account ID: ")
RF()
When the code is ran, I expect it to jump straight to asking for a name, but when I type "No" or "no", it for some reason runs the print("Your new ID Number is: " + str(ID)) from a different if statement
The statement if user_type is "Yes" or "yes": will always return true.
The reason for this is that the statement evaluates the same way as:
if (user_type is "Yes") or ("yes"):
The second parantheses ("yes") will always return true. And as #Error-SyntacticalRemorse pointed out, you want to use == instead of is (see Is there a difference between "==" and "is"?).
Solution 1:
if user_type in ["Yes", "yes"]:
Solution 2:
if user_type.lower() in ["yes", "y"]:
user_type = input("Are you a new user? ")
if user_type in ("Yes", "yes"):
ID = random.randrange(1, 999999)
print("Your new ID Number is: " + str(ID))
name = input("Please enter your name: ")
password = input("Password: ")
ID = input("Account ID: ")
writtingID = open('Acc info.txt', 'w')
writtingID.write("ID: " + str(ID) + " | NAME: " + name + " | PASS: " + password)
writtingID.close()
elif user_type in ("No", "no"):
name = input("Please enter your name: ")
password = input("Password: ")
ID = input("Account ID: ")
RF()
Check if the word is in a tuple. For an easier approach, just use the str.lower() method to get rid of the extra complexity.
AKA:
if user_type.lower() == "yes":
code
See how to test multiple variables against a value

Odd syntax Error that I do not know how to fix

I was creating a program in python 3 that asks your name, age, and a password. I am getting a syntax error and I do not know why.
Here is my code:
import time
needCheck = False
check = ("Just to check")
myName = ("Mason")
myAge = (12)
thePassword = ("Potatos are gud")
print("...")
time.sleep(3)
print("Scanning")
time.sleep(3)
print("*HUMAN_LIFEFORM DETECTED*")
time.sleep(2)
print("Greetings inhabitant of planet Earth")
name = input("What is your name, Earthling?")
if name == myName:
print("Oh, it's you")
needCheck = True
else:
print("Salutations, " + name + "!")
if needCheck:
age = input(check + ", " "what is your age?")
if age == myAge:
print("Looking good so far...")
else:
print("You're not fooling me anymore...")
needCheck = False
else:
age = input("What is your age " + name + "?")
if age == myAge:
print("Hmmm...")
if needCheck:
pass = input(check + "again" + ", " + "what is the super secret password?")
if pass == thePassword:
print("I knew it was you!")
else:
print("You must be an imposter! Where is the real Mason?!")
else:
pass = input("What is the super secret password, " + name + "?")
if pass = thePassword:
print("How did you know that?!")
print("Self Destruct In:")
time.sleep(1)
print(3)
time.sleep(1)
print(2)
time.sleep(1)
print(1)
while True:
print(BOOM)
time.sleep(0.5)
IDLE says the error is in this line, on the equals sign:
pass = input(check + "again" + ", " + "what is the super secret password")
If you know why it is invalid syntax, please answer.
Thanks!
pass is a reserved word. try using any other word and it should work

Python - how to sort a csv file using a list

I have placed data in a separate file from a quiz the user has done (by the way in the quiz the user selects one class out of three). Now I would like to sort that data which is in the file by alphbetical, the highest score and the average score for each class.
if MyClass == "1":
myFile = open('Class1.csv', 'a')
myFile.write("Class = ")
myFile.write(MyClass + " ")
myFile.write("Name = ")
myFile.write(name + " ")
myFile.write("Score = ")
myFile.write(str(score) + " ")
myFile.write("\n")
myFile.close()
elif MyClass == "2":
myFile = open('Class2.csv', 'a')
myFile.write("Class = ")
myFile.write(MyClass + " ")
myFile.write("Name = ")
myFile.write(name + " ")
myFile.write("Score = ")
myFile.write(str(score) + " ")
myFile.write("\n")
myFile.close()
elif MyClass == "3":
myFile = open('Class3.csv', 'a')
myFile.write("Class = ")
myFile.write(MyClass + " ")
myFile.write("Name = ")
myFile.write(name + " ")
myFile.write("Score = ")
myFile.write(str(score) + " ")
myFile.write("\n")
myFile.close()
this code is for saving the class into the decided file and the code below is for deciding which class they are in
def classes():
#here its asking the question to the user
theClass = input ("What class are you in: 1,2,3 ")
# Test the input is between 1 and 3
while (int(theClass) > 3):
theClass = input ("Sorry, please enter a number between 1 and 3 ")

Categories