Password generator not working - password-generator

I am a python noob and I don't know what's wrong with my code. Whenever I run it it just prints out "this is your password: " with nothing after that when it is supposed to print out the generated password.
import random
strength = ['Weak', 'Medium', 'Strong']
charbank = ('1234567890qwertyuiopASDFGHJKLZXCVBNM')
chosenchars = ('')
choice = ('')
def inputfunction():
while True:
userchoice = input("Would you like your password to be: \n Weak \n Medium? \n Strong?\n")
if userchoice in strength:
choice = ''.join(userchoice)
break
print ('oops, that\'s not one of the options. Enter again...')
return choice
def strengththing():
if choice == ("Weak"):
Weak()
if choice == ("Medium"):
Medium()
if choice == ("Strong"):
Strong()
def Weak():
passlen = 5
chosenchars.join(random.sample(charbank, passlen))
def Medium():
passlen = 10
chosenchars.join(random.sample(charbank, passlen))
def Strong():
passlen = 15
chosenchars.join(random.sample(charbank, passlen))
inputfunction()
strengththing()
print ('this is your password: %s' %chosenchars)
Any help would be great. I don't know where I went wrong. Thank you!

You didn't change the value of 'choice' with your return statement after your While loop.
I did a few modifications.
There you go :
#!/usr/bin/env python3
import random
strength = ['Weak', 'Medium', 'Strong']
charbank = ('1234567890qwertyuiopASDFGHJKLZXCVBNM')
chosenchars = ('')
choice = ('')
def inputfunction():
while True:
userchoice = input("Would you like your password to be: \n Weak \n Medium? \n Strong?\n")
if userchoice in strength:
choice = ''.join(userchoice)
break
else:
print ('oops, that\'s not one of the options. Enter again...')
return choice
def strengththing():
if choice == 'Weak':
return RandomPass(5)
if choice == 'Medium':
return RandomPass(10)
if choice == 'Strong':
return RandomPass(15)
def RandomPass(passlen):
myVar = ''.join(random.sample(charbank, passlen))
return myVar
choice = inputfunction()
chosenchars = strengththing()
print ('this is your password: %s' %chosenchars)

Related

Ruby factorial loop

Am I doing this wrong? I am trying to write a simple program to ask the user to enter a number until they wish to quit, but when I run the program it gives me an argument error stating bad value for range. what can I do to fix this problem?
var = true
while var == true
print "Enter a number ('Q/q to quit'): "
input = gets.chomp
if input == 'Q' || input == 'q'
var = false
puts "Goodbye!"
else
product = 1
for i in 1..input
product = product * i
end
end
end
puts "#{input}! is #{product}"
The problem is "input" is a string. You can't use it in your range. You need to convert it to a int.
change this:
for i in 1..input.to_i

How can I terminate this Ruby `while` loop when the user inputs "no"?

I'm working on a program that gives the user two random numbers ranging from 0 to 10 for the user to divide, multiply, add, or subtract.
After every question, the user has the opportunity to stop the program by typing no.
I am using a while loop for this, but I can't get the loop to terminate when the user inputs no. How can I get the program to respond correctly to the user input?
def math_num
nums = [num_1 = rand(1..10), num_2 = rand(1..10), operator = ["+", "-", "/", "*"].sample]
problem = "What is #{num_1} #{operator} #{num_2}?"
puts problem
$input = gets.to_i
$answer = num_1.send(operator, num_2)
puts $input == $answer ? "You answered #{$input}, and the answer is #{$answer}! You are correct!" : "The answer is #{$answer}, not #{$input}! You are incorrect!"
def try_again
puts "Would you like to do another question?"
another = gets.chomp.to_s
while another != "no"
math_num
end
end
try_again
end
math_num
Well, the way you are doing it you are getting an infinite loop since the value of the another variable is not being updated inside the while loop.
Try this instead:
def math_num
while true
nums = [num_1 = rand(1..10), num_2 = rand(1..10), operator = ["+", "-", "/","*"].sample]
problem = "What is #{num_1} #{operator} #{num_2}?"
puts problem
$input = gets.to_i
$answer = num_1.send(operator, num_2)
puts $input == $answer ? "You answered #{$input}, and the answer is #{$answer}! You are correct!" : "The answer is #{$answer}, not #{$input}! You are incorrect!"
puts "Would you like to do another question?"
another = gets.chomp.to_s
if another == "no"
break
end
end
end
math_num

Conversion to integer isn't executing?

i have this code :
#require_relative '../lib/hackex/net/typhoeus'
require_relative '../lib/hackex'
require 'rubygems'
require 'faker'
print "how many farms do you want : "
choice = gets.chomp
choice.to_i
check = choice.is_a?(Integer)
puts check
if choice > 250
puts "Error! you cannot make more than 250 farms at once"
elsif choice < 250
puts "Error! you have to make at least one farm..."
elsif choice.is_a?(Integer) == false
puts "Error, something went wrong !"
else
puts "making #{choice} acounts ! ! !"
cnt = 1
while choice>cnt
gen = Faker::Name.first_name + Faker::Name.last_name
path=("created1.txt")
email = gen+'#gmail.com'
password = Faker::Internet.password(8)
username = gen.to_s+'/>'
puts HackEx::Request.Do(http,HackEx::Request.CreateUser(username, email, password, facebook_id = nil))
cnt +=1
open(path, 'a') { |f|
f << "#{email};"
f << "#{password}"
f << "\n"
}
puts "Account created!"
puts "#{choice - cnt} accounts remaining!"
end
end
i am trying to determing if the choice is an integer... i did the .to_i on choice, but it returns false, meaning its not an integer, its a string, why isnt it switching ?
ps : i do not get any errors, and the rest of the code works fine, except for the if part
choice.to_i returns an integer, but does not change choice. If you want choice to be changed to the integral value of the old choice, you need to reassign it explicitly:
choice = choice.to_i
Quoting the doc of String::to_i, emphasis is mine
to_i(base=10) → integer
Returns the result of interpreting leading characters in str as an
integer base base (between 2 and 36).
So you have to assign the return to something, or itself:
choice = choice.to_i

Ruby code efficiency

Is there a way to make this code shorter and simpler?
loop do
if possibleSet.split(" ").map(&:to_i).any? {|e| (e<0 || e>12)}
print "Please enter valid numbers (between 1 and 12): "
possibleSet = gets
errorinput = false
else
errorinput = true
end
break if errorinput
end
Refactored a bit :)
loop do
print "Please enter valid numbers (between 1 and 12): "
possibleSet = gets.chomp
break unless possibleSet.split(" ").map(&:to_i).any? {|e| (e<0 || e>12)}
end
The code below will check input for correctness:
input = loop do
print "Please enter valid numbers (between 1 and 12): "
# ⇓⇓⇓ as many spaces as user wants
input = gets.chomp.split(/\s+/).map(&:to_i) rescue []
break input unless input.empty? || input.any? { |i| !(0..12).include? i }
end
This parses the user input in an array (not exactly the same behavior, but I hope it is cleaner and you can work from there)
set = []
until set.all? {|i| (1..11).include?(i) } && !set.empty? do
set = gets.split(' ').map(&:to_i)
end

Replace string with ruby code

I worte an simple programm to replace text within (( )) with the user input:
If i have for example this this text:
i hab an terrible ((userinput1)) last ((userinput2)) in a horrible ((userinput3))
I tried first to replace the (( with #{ and the )) with }
str1 = gets.chomp
str2 = str1.clone
a = 0
begin
s = str2.index('((', a)
str2[s..s+1] = '#{'
a = a + s + 1
end until str2.length < a
b = 0
begin
s = str2.index('))', b)
str2[s..s+1] = '}'
b = b + s + 1
end until str2.length < b
userinput1 = gets.chomp
userinput2 = gets.chomp
userinput3 = gets.chomp
puts str2
But somehow ruby dont validates the userinputs, instead i get:
i hab an terrible #{userinput1} last #{userinput2} in a horrible #{userinput}
I think the problem is that in my code i wrote:
str2[s..s+1] = '#{'
instead of
str2[s..s+1] = "#{"
because so all my remaining code is an object until it is closed with }. SO what can i do ? I hope you understood my issue?
str1 = gets.chomp
word = str1.scan(/\(\(\w+\)\)/)
word.each do |word|
str1.gsub(word, "what the fuck")
end
puts str1
Nice try. But there's a simpler way. Much simpler :) Basically, you just have to collect user inputs. There are methods for replacing string parts already.
userinput1 = gets.chomp
userinput2 = gets.chomp
userinput3 = gets.chomp
template = "i had a terrible ((userinput1)) last ((userinput2)) in a horrible ((userinput3)"
result = template.gsub('((userinput1))', userinput1).
gsub('((userinput2))', userinput2).
gsub('((userinput3))', userinput3)
# ^^ replacing happens here
puts result

Resources