This post was returned to Unix & Linux Stack Exchange. It is not currently accepting new answers or interactions. Learn more
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 last month.
#!/usr/bin/env python3
# tarchiver.py
# Purpose: Creates a tar archive of a directory
#
# USAGE: ./tarchiver.py
#
# Author:
# Date January 15th 2023
import os
correct_answer = 'yes'
correct_answer2 = 'no'
compression1 = 'gzip'
compression2 = 'bzip2'
compression3 = 'xzip'
print("Please enter the directory you would like to archive")
directory = input()
print("Please enter the name of the archive")
name = input()
print("Would you like your archive to be compressed?")
answer = input()
while correct_answer != answer or correct_answer2 != answer:
answer = input()
print('Please enter either yes or no')
if answer == correct_answer or answer == correct_answer2:
break
if answer == 'yes':
print("What kind of compression do you want?")
print("gzip, bzip2, or xzip?")
answer2 = input()
while compression1 != answer2 or compression2 != answer2 or compression3 != answer2:
print('Please enter a valid answer')
answer2 = input()
if answer2 == compression1 or answer == compression2 or answer == compression3:
break
if answer2 == "gzip":
os.system(f"tar -cvPzf {name} {directory}")
if answer2 == "bzip2":
os.system(f"tar -cvPjf {name} {directory}")
if answer2 == "xzip":
os.system(f"tar -cvPJf {name} {directory}")
I'm having trouble with the logic in the code. When it asks whether or not I would like compression and I type 'yes', I have to type it twice in order for the code to proceed to the next section. Also, when it asks for type and I input 'gzip', it tells me at first that it's an invalid input and that I need to correct my answer, but I just enter the same thing and then it proceeds to execute the rest of the code. This is for a school project and I'm new to python so excuse me if there is an obvious solution to this problem.
Related
okay so im new to coding in general and during a practice activity i ran into a problem with using while loops, im trying to simulate a 8-ball that doesnt work unless you ask it a question, so far all im trying to do is to cause the code to re ask the question until it meets the peramiters of a none empty input but everytime now that the input isnt empty it just ends before printing out the 8-balls answer
question = input("Isaiah's MagicAndTotallyNotSentient 8-Ball: what is your question? ")
while len(question) == 0:
if len(question) == 0:
print("...")
question = input("Isaiah's MagicAndTotallyNotSentient 8-Ball: What is your question?")
continue
elif len(question) < 0:
break
ive been at this all day yesterday and today ive finally got the loop to end in general but now i dont know how to get it to continue to execute the code after my while loop without breaking the current loop i have in place ive tried using an else statement to break the loop and this elif statment to break the loop but now im not quite sure what to do
You can utilize walrus operator (:=) to assign and evaluate a variable and also reduce repeated code. So the while loop will keep continue until the value of the variable is not None or not empty:
while not (question := input("Isaiah's MagicAndTotallyNotSentient 8-Ball: what is your question? ")):
print("...")
# next code
I think it would be more practical if you just check if the input is empty, instead of enter a while loop right away.
Something like this:
question = input("Isaiah's MagicAndTotallyNotSentient 8-Ball: what is your
question? ")
if len(question) == 0:
while len(question) == 0:
print("...")
question = input("Isaiah's MagicAndTotallyNotSentient 8-Ball: What is your question?")
print("The answer to your question is...etc")
#(rest of the code)
You only need to enter the loop if the input is empty. And going if len(question) == 0: and while len(question) == 0: seems redundant.
I think I'd do it like this:
question = input("Isaiah's MagicAndTotallyNotSentient 8-Ball: What is your question?")
while len(question) == 0:
print("...")
question = input("Isaiah's MagicAndTotallyNotSentient 8-Ball: What is your question?")
print("The answer is:")
Basic Python Chatbot
An Objective of the task is to create a Basic Python chatbot where any questions asked if the chatbot does not know the answer, it shows request the user to provide the answers. Once the answer is received, it should write that question and its answer it into the pandas dataframe. In the future, a similar question is asked, the chatbot should look into the pandas data frame and give the answer.
Purpose of creating the pandas data frame is that currently, I don't have any ready question and answer, so as time progress I will be adding the question and answer to pandas data frame one by one.
Blockquote
username = "User"
chatbotname = "<>"
chatbotnameknown = False
active = True
def saychatbot(text):
global username
global chatbotname
global chatbotnameknown
global active
if chatbotnameknown:
print(chatbotname + ": " + text)
else:
print("*: " + text)
def speak(user_entry):
global username
global chatbotname
global chatbotnameknown
global active
if user_entry == "Hello!" or user_entry == "hello":
saychatbot("Hi, " + username)
elif user_entry == "How are you?":
saychatbot("I'm fine. And you?")
reply = input("Your answer: ")
if reply == "Great":
saychatbot("I'm glad to hear that.")
else:
saychatbot("I didn't understand you.")
elif user_entry == "Bye":
active = False
else:
saychatbot("I didn't understand you.")
saychatbot("I am still learning, let me learn your language")
if input("Would you like to teach me your language, Say y/n ? ") == "y":
saychatbot("You know i am still infancy, so please teach me your language one question and its answer at a time so i will load it in my database!!")
print("Here I would like to record the question and its answer in Pandas data frame and use that data frame as input to answer the same question in future")
print("Is there any way to achieve it")
def OpenDiscussion():
global username
global chatbotname
global chatbotnameknown
global active
print("********Python - Do you know system can speak****************")
while active:
if chatbotnameknown:
speak(str(input(username + ": " + chatbotname + ", ")))
else:
speak(str(input(username + ": ")))
saychatbot("Bye.")
OpenDiscussion()
First, you can set up a data structure to store the <question, answer> data. In this case, a dictionary would be fine.
If you need use dataframe and add data, pandas.DataFrame.append method would help.
I am trying to set up a fighting game where it prints a question and you have to answer the question correctly to win the fight. But I a finding that I can't find a way to get the code t read the random question to be read thus meaning I can't get it to read the answer as correct.
I've tried making the random be separated into multiple variables but that didn't work. I haven't had much time to try anything else either.
import random
fights=("I run but never walk, I have a bed but never sleep", "What time
of day is the same fowards as it is backwards?", "3+2")
FIGHTS=random.choice(fights)
print(FIGHTS)
ans1="river"
ans2="noon"
ans3="5"
que1=input("What shall you say?\n")
if ans1=="I run but never walk, I have a bed but never sleep":
print("You won!")
elif ans2=="What time of day is the same fowards as it is backwards?":
print("You won!")
elif ans3=="3+2":
print("You won!")
else:
print("You lost...")
If you answer correctly it displays "You won!" and if you answer wrongly it displays "You lost..." but it can't read what is printed so it always displays "You lost..."
A good approach would be to store the questions and answers in a dictionary and use good variable names.
import random
fights = {"I have a bed but never sleep": "river",
"What time of day is the same fowards as it is backwards?": "noon",
"3+2": "5"}
question = random.choice(tuple(fights.keys()))
print(question )
answer = input("What shall you say?\n")
if answer == fights[question]:
print("correct")
else:
print("wrong")
If you are not going to use this dictionary again, you can use fights.popitem() instead. Keep in mind that if using Python >= 3.7 popitem will always return the same key-value pair:
fights = {"I have a bed but never sleep": "river",
"What time of day is the same fowards as it is backwards?": "noon",
"3+2": "5"}
question, correct_answer = fights.popitem()
print(question)
user_answer = input("What shall you say?\n")
if user_answer == correct_answer:
print("correct")
else:
print("wrong")
How can I have a multiple choice question with each answer a different response and a default answer for an answer that is not understandable?
I am trying to create a game where you use your imagination to complete the game.
I also need to somehow repeat until the correct answer is given.
Here is the code:
answer = input("start?")
while answer.lower() != "start":
answer = input("type start")
else:
print("starting")
answer = input("you find yourself in a car")
while answer != "["look", "get out"]:
print("I don't understand..")
answer = input("Try again")
else:
if answer == "look":
print ("looking")
elif answer == "get out":
print("got out of car")
print("finished")
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 8 years ago.
Improve this question
def shut_down(s):
s = s.Lower()
if s == 'yes' :
return "Shutting down..."
elif s == 'no':
return "Shutdown aborted!"
else :
return "Sorry, I didn't understand you."
the computer tell me that Your shut_down function threw the following error: 'str' object has no attribute 'Lower'
Your .Lower() is not available in python because it's case sensitive language use .lower()
def shut_down(s):
s = s.lower()
if s == 'yes' :
return "Shutting down..."
elif s == 'no':
return "Shutdown aborted!"
else :
return "Sorry, I didn't understand you."